ArrayAdapter可能导致应用程序崩溃 [英] ArrayAdapter possibly causes app to crash

查看:164
本文介绍了ArrayAdapter可能导致应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的应用程序向前迈进的问题。我刚开始开发的应用程序,只涉及约15小时为止。但是,我遇到了阻碍。通常我能够谷歌碰壁和我的大多数答案都来自这个论坛!

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!

所以我希望我能找到一个解决方案。下面是我的活动的java文件。这就是问题,因为它以前工作的罚款。我一直有与使用数组适配器显示我的名单一吨的麻烦。

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

}

现在我不知道究竟是什么引起我的问​​题。我曾尝试使用Google跟踪的各个部分都无济于事。我觉得就可以了许多文章,但每一个presents它完全不同于我有我所以他们的解决方案并不为我工作。下面是跟踪。

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

我将试图在此期间解决这一问题。我想我是唯一一个谁宁愿程序比观看超级碗:P

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

推荐答案

在你的类的顶部,保持

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

的onCreate() onPostExecute(),这取决于你想用这些数据做什么

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

该例外被抛出,因为适配器 S(和许多Android组件)需要一定的(幕后)的东西在活动初始​​化。但是,这些事情不会在活动发生之前的 的onCreate(),因为的onCreate()是活动的生命周期的开始。

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.

这篇关于ArrayAdapter可能导致应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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