Android 应用程序类中的ArrayList

发布于 2024-12-04 14:44:17 字数 6029 浏览 2 评论 0原文

我试图通过 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 ArrayLists 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

生生漫 2024-12-11 14:44:17

为什么需要创建一个空列表并再次添加项目?另外hProviders设置时是否为空?

public void setHProviders(ArrayList<Marchand> hProviders) {
        HProviders = hProviders;
}

Why do you need to create an empty list and add items again?? Also hProviders is it null when setting??

public void setHProviders(ArrayList<Marchand> hProviders) {
        HProviders = hProviders;
}
空名 2024-12-11 14:44:17

此行产生 NullPointerException

HProviders.addAll(hProviders);

因为当您调用时 Marchandnull

myApp.setHProviders(Marchands);

确保 Marchand 不为 null 或更好,遵循 Teja Kantamneni 的建议。

This line produces NullPointerException:

HProviders.addAll(hProviders);

because Marchand is null when you call

myApp.setHProviders(Marchands);

Make sure Marchand is not null, or better, follow advice of Teja Kantamneni.

预谋 2024-12-11 14:44:17

您需要在onCreate()中初始化变量myApp。当前,您正在创建实例时初始化此变量,如下所示:

appCyberesa myApp = ((appCyberesa)this.getApplication());

问题是,在创建 Splash 实例时,对 getApplication() 的调用将返回 null 因为必要的底层框架尚未设置(这一切都发生在调用 onCreate() 之前的构造函数中)。只需将初始化移至 onCreate() 即可。

You need to initialize the variable myApp in onCreate(). You are currently initializing this variable when the instance is created, like this:

appCyberesa myApp = ((appCyberesa)this.getApplication());

The problem is that at the time the instance of Splash is created, a call to getApplication() will return null because the necessary underlying framework hasn't been set up yet (this all happens in the constructor before onCreate() is called). Just move the initialization to onCreate() and you will be good.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文