views:

105

answers:

1

My Android application crashes when I try to call MyCursorAdapter.bindView. Here is the LogCat log:

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)

Here is the bindView method in my CursorAdapter:

    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());

Line 28 starts with body.setText(cursor.getString(cursor.getColumnIndex(TasksDBAdapter.Key_BODY))); Any other information needed?

UPDATE:

EditText body = (EditText) view.findViewById(R.id.body);

Seems to be null? Here is the widget:

<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>
+1  A: 

You have to get your reference from another view that the view parameter in bindView method. The view is just the row layout and not the whole layout. That's why when you do view.findViewById, it returns null because Android doesn't know about this id in this view.

fedj