因此,我尝试编写的这个程序很奇怪,它的作用是,它从站点下载一个JSON文件,解析它,然后将所有项目放入ListView
。关于它的奇怪之处在于,当我运行调试器时,我看到它成功填充了屏幕上的列表,但是当我尝试正常运行时它会立即崩溃。这是我从logcat获得的陈述:
03-30 22:34:28.115: E/AndroidRuntime(17758): FATAL EXCEPTION: main
03-30 22:34:28.115: E/AndroidRuntime(17758): Process: com.example.listview, PID: 17758
03-30 22:34:28.115: E/AndroidRuntime(17758): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.listview/com.example.listview.Activity_ListView}: java.lang.NullPointerException
03-30 22:34:28.115: E/AndroidRuntime(17758): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-30 22:34:28.115: E/AndroidRuntime(17758): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-30 22:34:28.115: E/AndroidRuntime(17758): at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-30 22:34:28.115: E/AndroidRuntime(17758): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-30 22:34:28.115: E/AndroidRuntime(17758): at android.os.Handler.dispatchMessage(Handler.java:102)
03-30 22:34:28.115: E/AndroidRuntime(17758): at android.os.Looper.loop(Looper.java:136)
03-30 22:34:28.115: E/AndroidRuntime(17758): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-30 22:34:28.115: E/AndroidRuntime(17758): at java.lang.reflect.Method.invokeNative(Native Method)
03-30 22:34:28.115: E/AndroidRuntime(17758): at java.lang.reflect.Method.invoke(Method.java:515)
03-30 22:34:28.115: E/AndroidRuntime(17758): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-30 22:34:28.115: E/AndroidRuntime(17758): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-30 22:34:28.115: E/AndroidRuntime(17758): at dalvik.system.NativeStart.main(Native Method)
03-30 22:34:28.115: E/AndroidRuntime(17758): Caused by: java.lang.NullPointerException
03-30 22:34:28.115: E/AndroidRuntime(17758): at com.example.listview.CustomAdapter.getCount(CustomAdapter.java:61)
03-30 22:34:28.115: E/AndroidRuntime(17758): at android.widget.ListView.setAdapter(ListView.java:480)
03-30 22:34:28.115: E/AndroidRuntime(17758): at android.app.ListActivity.setListAdapter(ListActivity.java:265)
03-30 22:34:28.115: E/AndroidRuntime(17758): at com.example.listview.Activity_ListView.onCreate(Activity_ListView.java:85)
03-30 22:34:28.115: E/AndroidRuntime(17758): at android.app.Activity.performCreate(Activity.java:5231)
03-30 22:34:28.115: E/AndroidRuntime(17758): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-30 22:34:28.115: E/AndroidRuntime(17758): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
感谢您的帮助!
答案 0 :(得分:2)
CustomAdapter.java文件61行错误。 自定义适配器创建一个移交参数的位置确保您的列表。 在适配器中出现nullpointEexception'm getCount方法。
答案 1 :(得分:1)
我在执行下载JSON的AsyncTask之后立即告诉onCreate中的setListAdapter。所以我通过在onPostExecute中设置列表适配器来修复它。哇,这是一个愚蠢的错误。但是感谢@Tamil Selvan他的问题让我意识到我的错!