Android ArrayList in Application Class

2019-08-18 08:11发布

问题:

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

回答1:

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


回答2:

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.



回答3:

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.