我NullPointerException在尝试启动包含的活动时花了一段时间ListView。
NullPointerException
ListView
在getView适配器类的方法中,当setText调用a 的函数时会发生异常textView。
getView
setText
textView
下面的代码是我的适配器类:
public class QuestionsListAdapter extends ArrayAdapter<Question> { Context context; List<Question> questions; public QuestionsListAdapter(Context context, List<Question> questions){ super(context, R.layout.list_item_question, questions); this.context = context; this.questions = questions; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = vi.inflate(R.layout.list_item_question, null); Question question = questions.get(position); TextView tv = (TextView) view.findViewById(R.id.question_list_item_string); Log.i(TableCreator.LOG_TAG, question.toString()); //this works fine and the string is not null . tv.setText(question.toString()+""); //NULL POINTER EXCEPTION . return view; } }
如您所见,我已经将字符串记录在logcat中,并且工作正常,但是下一行会出错。这是logcat的输出:
05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist I/Operation Checklist﹕ |-Question-> id:1 summary:mySummary comment:myComment solution:mySolution ownerList:dummyOwner 05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist D/AndroidRuntime﹕ Shutting down VM 05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb0f5f648) 05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.NullPointerException at org.kabiri.operationchecklist.adapter.QuestionsListAdapter.getView(QuestionsListAdapter.java:43) at android.widget.AbsListView.obtainView(AbsListView.java:2177) at android.widget.ListView.makeAndAddView(ListView.java:1840) at android.widget.ListView.fillDown(ListView.java:675) at android.widget.ListView.fillFromTop(ListView.java:736) at android.widget.ListView.layoutChildren(ListView.java:1655) at android.widget.AbsListView.onLayout(AbsListView.java:2012) at android.view.View.layout(View.java:14289) at android.view.ViewGroup.layout(ViewGroup.java:4562) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525) at android.widget.LinearLayout.onLayout(LinearLayout.java:1434) at android.view.View.layout(View.java:14289) at android.view.ViewGroup.layout(ViewGroup.java:4562) at android.widget.FrameLayout.onLayout(FrameLayout.java:448) at android.view.View.layout(View.java:14289) at android.view.ViewGroup.layout(ViewGroup.java:4562) at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:502) at android.view.View.layout(View.java:14289) at android.view.ViewGroup.layout(ViewGroup.java:4562) at android.widget.FrameLayout.onLayout(FrameLayout.java:448) at android.view.View.layout(View.java:14289) at android.view.ViewGroup.layout(ViewGroup.java:4562) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525) at android.widget.LinearLayout.onLayout(LinearLayout.java:1434) at android.view.View.layout(View.java:14289) at android.view.ViewGroup.layout(ViewGroup.java:4562) at android.widget.FrameLayout.onLayout(FrameLayout.java:448) at android.view.View.layout(View.java:14289) at android.view.ViewGroup.layout(ViewGroup.java:4562) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749) at android.view.Choreographer.doCallbacks(Choreographer.java:562) at android.view.Choreographer.doFrame(Choreographer.java:532) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) at android.os.Handler.handleCallback(Handler.java:730) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5103) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) at dalvik.system.NativeStart.main(Native Method)
日志显示该错误发生在我的适配器类的这一行:
tv.setText(question.toString()+"");
我非常感谢您的帮助。
您已经知道问题出在哪里了!
导致NPE的原因TextView tv是null。这意味着线
TextView tv
null
TextView tv = (TextView) view.findViewById(R.id.question_list_item_string);
无法找到TextView。检查question_list_item_stringID,并确保其与list_item_question.xml文件中的ID相匹配
question_list_item_string
list_item_question.xml