我在使用我的应用程序时遇到问题。我刚开始开发这个应用程序,到目前为止只用了大约 15 个小时。但我遇到了一个障碍。通常我可以用谷歌搜索障碍,我的大部分答案都来自这个论坛!
所以我希望我能找到解决方案。下面是我的活动 java 文件。这就是问题所在,因为它以前工作正常。使用数组适配器显示我的列表时遇到了很多麻烦。
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);
}
}
}
现在我不知道究竟是什么导致了我的问题。我试过用谷歌搜索跟踪的各个部分无济于事。我找到了很多关于它的文章,但每一篇都与我的完全不同,所以他们的解决方案对我不起作用。下面是痕迹。
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