应用程序类中的Android ArrayList [英] Android ArrayList in Application Class

查看:34
本文介绍了应用程序类中的Android ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 Application 类传递 ArrayLists,但是在填充这些列表时,即使列表已初始化,我也会得到空指针异常.我的应用程序类是:

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;
    }
}

以下是导致 NullPointerException 的原因:

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);

这是堆栈跟踪:

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);

and the ligne (Splash.java:70) is : myApp.setHProviders(Marchands);

@Peter : Hole splash.java :

@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;
        }
    }
}

推荐答案

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

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());

问题在于,在创建 Splash 的实例时,调用 getApplication() 将返回 null 因为必要底层框架尚未建立(这一切都发生在调用 onCreate() 之前的构造函数中).只需将初始化移动到 onCreate() 就可以了.

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.

这篇关于应用程序类中的Android ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆