Android 应用程序类中的ArrayList
我试图通过 Application
类传递 ArrayList,但是在填充这些列表时,即使列表已初始化,我也会收到 nullpointerexception。我的应用程序类是:
public class appCyberesa extends Application{
private ArrayList<Marchand> HProviders ;//= new ArrayList<Marchand>();
private ArrayList<CarAgency> CProviders ;//= new ArrayList<CarAgency>();
private ArrayList<Agency> Agencies ;//= new ArrayList<Agency>();
public GeoPoint Tunisie = new GeoPoint(microdegrees(36.80023),microdegrees(10.186073));
private GeoPoint myLoc;
@Override
public void onCreate() {
}
private static int microdegrees(double value){
return (int)(value*1000000);
}
public void setMyLoc(GeoPoint myLoc) {
this.myLoc = myLoc;
}
public GeoPoint getMyLoc() {
return myLoc;
}
public void setHProviders(ArrayList<Marchand> hProviders) {
HProviders = new ArrayList<Marchand>();
HProviders.addAll(hProviders);
}
public ArrayList<Marchand> getHProviders() {
return HProviders;
}
public void setCProviders(ArrayList<CarAgency> cProviders) {
CProviders = new ArrayList<CarAgency>();
CProviders.addAll(cProviders);
}
public ArrayList<CarAgency> getCProviders() {
return CProviders;
}
public void setAgencies(ArrayList<Agency> agencies) {
Agencies = new ArrayList<Agency>();
Agencies.addAll(agencies);
}
public ArrayList<Agency> getAgencies() {
return Agencies;
}
}
并且以下导致 NullPointerException :
appCyberesa myApp = ((appCyberesa)this.getApplication());
...
//the error occurs here
myApp.setHProviders(Marchands);
myApp.setCProviders(CarRenters);
myApp.setAgencies(Agencies);
这是堆栈跟踪:
09-15 13:59:19.284: ERROR/AndroidRuntime(761): java.lang.RuntimeException: An error occured while executing doInBackground()
09-15 13:59:19.284: ERROR/AndroidRuntime(761): at android.os.AsyncTask$3.done(AsyncTask.java:200)
09-15 13:59:19.284: ERROR/AndroidRuntime(761): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
09-15 13:59:19.284: ERROR/AndroidRuntime(761): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
09-15 13:59:19.284: ERROR/AndroidRuntime(761): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
09-15 13:59:19.284: ERROR/AndroidRuntime(761): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-15 13:59:19.284: ERROR/AndroidRuntime(761): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
09-15 13:59:19.284: ERROR/AndroidRuntime(761): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
09-15 13:59:19.284: ERROR/AndroidRuntime(761): at java.lang.Thread.run(Thread.java:1096)
09-15 13:59:19.284: ERROR/AndroidRuntime(761): Caused by: java.lang.NullPointerException
09-15 13:59:19.284: ERROR/AndroidRuntime(761): at com.cyberesa.info.Splash$ProgressTask.doInBackground(Splash.java:70)
09-15 13:59:19.284: ERROR/AndroidRuntime(761): at com.cyberesa.info.Splash$ProgressTask.doInBackground(Splash.java:1)
09-15 13:59:19.284: ERROR/AndroidRuntime(761): at android.os.AsyncTask$2.call(AsyncTask.java:185)
09-15 13:59:19.284: ERROR/AndroidRuntime(761): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-15 13:59:19.284: ERROR/AndroidRuntime(761): ... 4 more
并且 ligne (Splash.java:70) 是: myApp.setHProviders(Marchands);
@Peter :Hole rush.java :
package com.cyberesa.info;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
public class Splash extends Activity {
ArrayList<Marchand>Marchands = new ArrayList<Marchand>();
ArrayList<CarAgency> CarRenters = new ArrayList<CarAgency>();
ArrayList<Agency> Agencies = new ArrayList<Agency>();
appCyberesa myApp = ((appCyberesa)this.getApplication());
MyProgressDialog dialog;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.splashscreen);
dialog = MyProgressDialog.show(this, null, null);
new ProgressTask(Splash.this).execute();
}
private class ProgressTask extends AsyncTask<String, String, Boolean> {
//private Splash activity;
public ProgressTask(Splash splash) {
//this.activity = splash;
//context = splash;
}
/** application context. */
//private Context context;
protected void onPreExecute() {
}
protected void onCancelled (){
}
@Override
protected void onPostExecute(final Boolean success) {
dialog.dismiss();
Intent newIntent = new Intent(Splash.this, Main.class);
Splash.this.startActivity(newIntent);
}
protected void onProgressUpdate(String... msg) {
}
protected Boolean doInBackground(final String... args) {
Log.v("Splash","Loading Hotels providers");
HComparatorParser HParser = new HComparatorParser();
Marchands=HParser.parse("t");
Log.v("Splash","Loading Car providers");
CComparatorParser CParser = new CComparatorParser();
CarRenters = CParser.parse(0);
Log.v("Splash","Travel Agencies");
AgencyParser AParser=new AgencyParser();
Agencies=AParser.parse();
appCyberesa.setHProviders(Marchands);
appCyberesa.setCProviders(CarRenters);
appCyberesa.setAgencies(Agencies);
return true;
}
}
}
I'm trying to pass ArrayList
s over the Application
class, but when filling these lists, I get a nullpointerexception even if lists are initialized. My application class is :
public class appCyberesa extends Application{
private ArrayList<Marchand> HProviders ;//= new ArrayList<Marchand>();
private ArrayList<CarAgency> CProviders ;//= new ArrayList<CarAgency>();
private ArrayList<Agency> Agencies ;//= new ArrayList<Agency>();
public GeoPoint Tunisie = new GeoPoint(microdegrees(36.80023),microdegrees(10.186073));
private GeoPoint myLoc;
@Override
public void onCreate() {
}
private static int microdegrees(double value){
return (int)(value*1000000);
}
public void setMyLoc(GeoPoint myLoc) {
this.myLoc = myLoc;
}
public GeoPoint getMyLoc() {
return myLoc;
}
public void setHProviders(ArrayList<Marchand> hProviders) {
HProviders = new ArrayList<Marchand>();
HProviders.addAll(hProviders);
}
public ArrayList<Marchand> getHProviders() {
return HProviders;
}
public void setCProviders(ArrayList<CarAgency> cProviders) {
CProviders = new ArrayList<CarAgency>();
CProviders.addAll(cProviders);
}
public ArrayList<CarAgency> getCProviders() {
return CProviders;
}
public void setAgencies(ArrayList<Agency> agencies) {
Agencies = new ArrayList<Agency>();
Agencies.addAll(agencies);
}
public ArrayList<Agency> getAgencies() {
return Agencies;
}
}
and the following is causing the NullPointerException :
appCyberesa myApp = ((appCyberesa)this.getApplication());
...
//the error occurs here
myApp.setHProviders(Marchands);
myApp.setCProviders(CarRenters);
myApp.setAgencies(Agencies);
Here's the stacktrace :
09-15 13:59:19.284: ERROR/AndroidRuntime(761): java.lang.RuntimeException: An error occured while executing doInBackground()
09-15 13:59:19.284: ERROR/AndroidRuntime(761): at android.os.AsyncTask$3.done(AsyncTask.java:200)
09-15 13:59:19.284: ERROR/AndroidRuntime(761): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
09-15 13:59:19.284: ERROR/AndroidRuntime(761): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
09-15 13:59:19.284: ERROR/AndroidRuntime(761): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
09-15 13:59:19.284: ERROR/AndroidRuntime(761): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-15 13:59:19.284: ERROR/AndroidRuntime(761): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
09-15 13:59:19.284: ERROR/AndroidRuntime(761): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
09-15 13:59:19.284: ERROR/AndroidRuntime(761): at java.lang.Thread.run(Thread.java:1096)
09-15 13:59:19.284: ERROR/AndroidRuntime(761): Caused by: java.lang.NullPointerException
09-15 13:59:19.284: ERROR/AndroidRuntime(761): at com.cyberesa.info.Splash$ProgressTask.doInBackground(Splash.java:70)
09-15 13:59:19.284: ERROR/AndroidRuntime(761): at com.cyberesa.info.Splash$ProgressTask.doInBackground(Splash.java:1)
09-15 13:59:19.284: ERROR/AndroidRuntime(761): at android.os.AsyncTask$2.call(AsyncTask.java:185)
09-15 13:59:19.284: ERROR/AndroidRuntime(761): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-15 13:59:19.284: ERROR/AndroidRuntime(761): ... 4 more
and the ligne (Splash.java:70) is : myApp.setHProviders(Marchands);
@Peter : Hole splash.java :
package com.cyberesa.info;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
public class Splash extends Activity {
ArrayList<Marchand>Marchands = new ArrayList<Marchand>();
ArrayList<CarAgency> CarRenters = new ArrayList<CarAgency>();
ArrayList<Agency> Agencies = new ArrayList<Agency>();
appCyberesa myApp = ((appCyberesa)this.getApplication());
MyProgressDialog dialog;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.splashscreen);
dialog = MyProgressDialog.show(this, null, null);
new ProgressTask(Splash.this).execute();
}
private class ProgressTask extends AsyncTask<String, String, Boolean> {
//private Splash activity;
public ProgressTask(Splash splash) {
//this.activity = splash;
//context = splash;
}
/** application context. */
//private Context context;
protected void onPreExecute() {
}
protected void onCancelled (){
}
@Override
protected void onPostExecute(final Boolean success) {
dialog.dismiss();
Intent newIntent = new Intent(Splash.this, Main.class);
Splash.this.startActivity(newIntent);
}
protected void onProgressUpdate(String... msg) {
}
protected Boolean doInBackground(final String... args) {
Log.v("Splash","Loading Hotels providers");
HComparatorParser HParser = new HComparatorParser();
Marchands=HParser.parse("t");
Log.v("Splash","Loading Car providers");
CComparatorParser CParser = new CComparatorParser();
CarRenters = CParser.parse(0);
Log.v("Splash","Travel Agencies");
AgencyParser AParser=new AgencyParser();
Agencies=AParser.parse();
appCyberesa.setHProviders(Marchands);
appCyberesa.setCProviders(CarRenters);
appCyberesa.setAgencies(Agencies);
return true;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么需要创建一个空列表并再次添加项目?另外
hProviders
设置时是否为空?Why do you need to create an empty list and add items again?? Also
hProviders
is it null when setting??此行产生
NullPointerException
:因为当您调用时
Marchand
为null
确保
Marchand
不为 null 或更好,遵循 Teja Kantamneni 的建议。This line produces
NullPointerException
:because
Marchand
isnull
when you callMake sure
Marchand
is not null, or better, follow advice of Teja Kantamneni.您需要在
onCreate()
中初始化变量myApp
。当前,您正在创建实例时初始化此变量,如下所示:问题是,在创建
Splash
实例时,对getApplication()
的调用将返回null
因为必要的底层框架尚未设置(这一切都发生在调用onCreate()
之前的构造函数中)。只需将初始化移至onCreate()
即可。You need to initialize the variable
myApp
inonCreate()
. You are currently initializing this variable when the instance is created, like this:The problem is that at the time the instance of
Splash
is created, a call togetApplication()
will returnnull
because the necessary underlying framework hasn't been set up yet (this all happens in the constructor beforeonCreate()
is called). Just move the initialization toonCreate()
and you will be good.