空指针异常上下文为空
Posted
技术标签:
【中文标题】空指针异常上下文为空【英文标题】:Null Pointer Exception context is null 【发布时间】:2015-11-14 11:12:32 【问题描述】: @Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_postlist);
ListView listView = (ListView) this.findViewById(R.id.postListView);
PostItemAdapter itemAdapter = new PostItemAdapter(this,
R.layout.list, listData);
//this.accessWebService();
listView.setAdapter(itemAdapter);
03-15 16:11:12.104: E/androidRuntime(844): FATAL EXCEPTION: main
03-15 16:11:12.104: E/AndroidRuntime(844): java.lang.RuntimeException: Unable to start activity ComponentInfoie.ucc.bis.is4432calculator/ie.ucc.bis.is4432calculator.secondActivity: java.lang.NullPointerException
03-15 16:11:12.104: E/AndroidRuntime(844): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-15 16:11:12.104: E/AndroidRuntime(844): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-15 16:11:12.104: E/AndroidRuntime(844): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-15 16:11:12.104: E/AndroidRuntime(844): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-15 16:11:12.104: E/AndroidRuntime(844): at android.os.Handler.dispatchMessage(Handler.java:99)
03-15 16:11:12.104: E/AndroidRuntime(844): at android.os.Looper.loop(Looper.java:123)
03-15 16:11:12.104: E/AndroidRuntime(844): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-15 16:11:12.104: E/AndroidRuntime(844): at java.lang.reflect.Method.invokeNative(Native Method)
03-15 16:11:12.104: E/AndroidRuntime(844): at java.lang.reflect.Method.invoke(Method.java:507)
03-15 16:11:12.104: E/AndroidRuntime(844): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-15 16:11:12.104: E/AndroidRuntime(844): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-15 16:11:12.104: E/AndroidRuntime(844): at dalvik.system.NativeStart.main(Native Method)
03-15 16:11:12.104: E/AndroidRuntime(844): Caused by: java.lang.NullPointerException
03-15 16:11:12.104: E/AndroidRuntime(844): at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:291)
03-15 16:11:12.104: E/AndroidRuntime(844): at android.widget.ListView.setAdapter(ListView.java:454)
03-15 16:11:12.104: E/AndroidRuntime(844): at ie.ucc.bis.is4432calculator.secondActivity.onCreate(secondActivity.java:42)
03-15 16:11:12.104: E/AndroidRuntime(844): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-15 16:11:12.104: E/AndroidRuntime(844): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 03-15 16:11:12.104: E/AndroidRuntime(844): ... 11 更多
如何使用intent作为适配器请帮忙.............
这些都是错误
【问题讨论】:
你能说得清楚一点吗?发布错误?您尝试使用的 Intent 在哪里? 。我想使用意图将数组发送到新活动 发布您的适配器代码。这就是错误所在。错误....secondActivity
的第 42 行
【参考方案1】:
listData
可能是null
。这就是ArrayAdapter.getCount()
抛出 NPE 的最可能原因。
【讨论】:
你能告诉我如何删除 NPE 确保listData
实际分配了对数据的引用。以上是关于空指针异常上下文为空的主要内容,如果未能解决你的问题,请参考以下文章