Mark
Mark

Reputation: 927

ArrayAdapter possibly causes app to crash

I am having a problem moving forward with my app. I just started developing the app and only have about 15 hours involved so far. But I have hit a snag. Usually I am able to google snags and most of my answers come from this forum!

so I hope I can find a solution for this. Below is my activity java file. This is where the issue is as it was working fine before. I have been having a ton of trouble with showing my list using an array adapter.

public class Favorites extends Activity{
UserFunctions userFunctions  = new UserFunctions();
ArrayList<String> zipcodes = new ArrayList<String>(0);
ArrayAdapter<String> arrayAdapter1 =      
         new ArrayAdapter<String>(this,android.R.layout.activity_list_item,zipcodes);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.favoritespage);

    new DownloadDataTask().execute();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main_screen, menu);
    return true;
}

   private class DownloadDataTask extends AsyncTask<JSONArray, JSONArray, ArrayList<String> > {


        @Override
        protected ArrayList<String> doInBackground(JSONArray... params) {
            JSONArray json = userFunctions.ziplistrequest("37.5", "140.45", "20");

            for(int i=0; i < json.length() ; i++) {
                JSONObject jarray = null;
                try {
                    jarray = json.getJSONObject(i);
                    String zip = jarray.getString("ZIPCODE");
                    zipcodes.add(zip);
                    arrayAdapter1.add(zip);
                    Log.d(zip,"Output");
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            return zipcodes;
        }   
        protected void onPostExecute(){
            ListView listView = (ListView) findViewById(R.id.listView1);
            listView.setAdapter(arrayAdapter1);
        }
    }

}

Now I do not know what is causing my problem exactly. I have tried googling individual parts of the trace to no avail. I find many articles on it but each one presents it completely different than I have mine so their solution does not work for me. Below is the trace.

01-29 07:55:47.753: E/AndroidRuntime(3723): FATAL EXCEPTION: main
01-29 07:55:47.753: E/AndroidRuntime(3723): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.lo/com.example.lo.Favorites}: java.lang.IllegalStateException: System services not available to Activities before onCreate()
01-29 07:55:47.753: E/AndroidRuntime(3723):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
01-29 07:55:47.753: E/AndroidRuntime(3723):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-29 07:55:47.753: E/AndroidRuntime(3723):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-29 07:55:47.753: E/AndroidRuntime(3723):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-29 07:55:47.753: E/AndroidRuntime(3723):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-29 07:55:47.753: E/AndroidRuntime(3723):     at android.os.Looper.loop(Looper.java:137)
01-29 07:55:47.753: E/AndroidRuntime(3723):     at android.app.ActivityThread.main(ActivityThread.java:5039)
01-29 07:55:47.753: E/AndroidRuntime(3723):     at java.lang.reflect.Method.invokeNative(Native Method)
01-29 07:55:47.753: E/AndroidRuntime(3723):     at java.lang.reflect.Method.invoke(Method.java:511)
01-29 07:55:47.753: E/AndroidRuntime(3723):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-29 07:55:47.753: E/AndroidRuntime(3723):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-29 07:55:47.753: E/AndroidRuntime(3723):     at dalvik.system.NativeStart.main(Native Method)
01-29 07:55:47.753: E/AndroidRuntime(3723): Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
01-29 07:55:47.753: E/AndroidRuntime(3723):     at android.app.Activity.getSystemService(Activity.java:4463)
01-29 07:55:47.753: E/AndroidRuntime(3723):     at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
01-29 07:55:47.753: E/AndroidRuntime(3723):     at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:104)
01-29 07:55:47.753: E/AndroidRuntime(3723):     at com.example.lo.Favorites.<init>(Favorites.java:33)
01-29 07:55:47.753: E/AndroidRuntime(3723):     at java.lang.Class.newInstanceImpl(Native Method)
01-29 07:55:47.753: E/AndroidRuntime(3723):     at java.lang.Class.newInstance(Class.java:1319)
01-29 07:55:47.753: E/AndroidRuntime(3723):     at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
01-29 07:55:47.753: E/AndroidRuntime(3723):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
01-29 07:55:47.753: E/AndroidRuntime(3723):     ... 11 more

I will be trying to fix this in the meantime. I think I am the only one who would rather program than watch the superbowl :P

Upvotes: 0

Views: 2754

Answers (2)

A--C
A--C

Reputation: 36449

At the top of your class, keep

ArrayAdapter<String> arrayAdapter1; 

and move the rest so it looks like this:

arrayAdapter1 = new ArrayAdapter<String>(Favorites.this,android.R.layout.activity_list_item,zipcodes);

to onCreate() or in onPostExecute(), depending on what exactly you want to do with the data.

The exception is thrown because Adapters (and many Android components) need certain (behind the scenes) things initialized in the Activity. However, these things don't happen before the Activity's onCreate() is called since onCreate() is the beginning of the Activity's lifecyle.

Upvotes: 1

Ted Hopp
Ted Hopp

Reputation: 234795

The problem is this line:

ArrayAdapter<String> arrayAdapter1 =      
     new ArrayAdapter<String>(this,android.R.layout.activity_list_item,zipcodes);

You cannot use this in a call to new ArrayAdapter() until the activity has been initialized properly. You can assign to arrayAdapter1 inside onCreate() (after calling super.onCreate(), or inside your DownloadDataTask. It would be better to do this inside onPostExecute instead of in doInBackground.

Upvotes: 0

Related Questions