当我尝试调用MyCursorAdapter.bindView时,我的Android应用程序崩溃。这是LogCat日志:
08-10 15:22:57.269: ERROR/AndroidRuntime(463): FATAL EXCEPTION: main
08-10 15:22:57.269: ERROR/AndroidRuntime(463): java.lang.NullPointerException
08-10 15:22:57.269: ERROR/AndroidRuntime(463): at com.mohit.gtodo.TasksCursorAdapter.bindView(TasksCursorAdapter.java:28)
08-10 15:22:57.269: ERROR/AndroidRuntime(463): at android.widget.CursorAdapter.getView(CursorAdapter.java:186)
08-10 15:22:57.269: ERROR/AndroidRuntime(463): at android.widget.AbsListView.obtainView(AbsListView.java:1315)
08-10 15:22:57.269: ERROR/AndroidRuntime(463): at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
08-10 15:22:57.269: ERROR/AndroidRuntime(463): at android.widget.ListView.onMeasure(ListView.java:1109)
08-10 15:22:57.269: ERROR/AndroidRuntime(463): at android.view.View.measure(View.java:8171)
08-10 15:22:57.269: ERROR/AndroidRuntime(463): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
08-10 15:22:57.269: ERROR/AndroidRuntime(463): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
08-10 15:22:57.269: ERROR/AndroidRuntime(463): at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
08-10 15:22:57.269: ERROR/AndroidRuntime(463): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
08-10 15:22:57.269: ERROR/AndroidRuntime(463): at android.view.View.measure(View.java:8171)
08-10 15:22:57.269: ERROR/AndroidRuntime(463): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
08-10 15:22:57.269: ERROR/AndroidRuntime(463): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-10 15:22:57.269: ERROR/AndroidRuntime(463): at android.view.View.measure(View.java:8171)
08-10 15:22:57.269: ERROR/AndroidRuntime(463): at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
08-10 15:22:57.269: ERROR/AndroidRuntime(463): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
08-10 15:22:57.269: ERROR/AndroidRuntime(463): at android.view.View.measure(View.java:8171)
08-10 15:22:57.269: ERROR/AndroidRuntime(463): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
08-10 15:22:57.269: ERROR/AndroidRuntime(463): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-10 15:22:57.269: ERROR/AndroidRuntime(463): at android.view.View.measure(View.java:8171)
08-10 15:22:57.269: ERROR/AndroidRuntime(463): at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
08-10 15:22:57.269: ERROR/AndroidRuntime(463): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
08-10 15:22:57.269: ERROR/AndroidRuntime(463): at android.os.Handler.dispatchMessage(Handler.java:99)
08-10 15:22:57.269: ERROR/AndroidRuntime(463): at android.os.Looper.loop(Looper.java:123)
08-10 15:22:57.269: ERROR/AndroidRuntime(463): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-10 15:22:57.269: ERROR/AndroidRuntime(463): at java.lang.reflect.Method.invokeNative(Native Method)
08-10 15:22:57.269: ERROR/AndroidRuntime(463): at java.lang.reflect.Method.invoke(Method.java:521)
08-10 15:22:57.269: ERROR/AndroidRuntime(463): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-10 15:22:57.269: ERROR/AndroidRuntime(463): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-10 15:22:57.269: ERROR/AndroidRuntime(463): at dalvik.system.NativeStart.main(Native Method)
这是我的CursorAdapter中的bindView方法:
TextView title = (TextView) view.findViewById(R.id.title);
EditText body = (EditText) view.findViewById(R.id.body);
TextView created = (TextView) view.findViewById(R.id.created);
title.setText(cursor.getString(cursor.getColumnIndex(TasksDBAdapter.KEY_TITLE)));
title.setSelected(cursor.getInt(cursor.getColumnIndex(TasksDBAdapter.KEY_COMPLETED)) > 0);
body.setText(cursor.getString(cursor.getColumnIndex(TasksDBAdapter.KEY_BODY)));
created.setText(((Long)cursor.getLong(cursor.getColumnIndex(TasksDBAdapter.KEY_CREATED))).toString());
第28行以
body.setText(cursor.getString(cursor.getColumnIndex(TasksDBAdapter.Key_BODY)));
开头还需要其他信息吗?
更新:
EditText body = (EditText) view.findViewById(R.id.body);
似乎为空?这是小部件:
<EditText android:id="@+id/body" android:layout_below="@+id/body_txt" android:text="" android:layout_width="fill_parent" android:layout_height="wrap_content" android:minLines="10"></EditText>
最佳答案
您必须从另一个视图(bindView方法中的view参数)获取引用。
该视图仅是行布局,而不是整个布局。
这就是为什么当您执行view.findViewById时,它返回null的原因,因为Android在此视图中不知道此id。