Search code examples
androidcursoradapterlistadapter

Custom list cursor adaptor crashes at bindView on emulator -- not on phone


I will try to be specific if I can - please be patient, first time asker and relatively new to programming on this platform. Apologies if this has been asked/answered before - please link it to me. I have searched up and down but find other unrelated (to me at least) problems.

The real puzzler for me is that my app is crashing on my emulator but when installed on my phone (via upload of apk to phone and then using an AppInstaller app from market) it works.

The crash comes from a NullPointerException at the numbered line in the following code snippet (my code) of a custom list cursor adapter.

// TaskListCursorAdapter.java

@Override
public void bindView(View view, Context context, Cursor cursor) {

    super.bindView(view, context, cursor); // <<< LINE 36
    // DO OTHER BINDING OF STRINGS TO TEXT VIEWS ETC

The following error dump is created.

08-23 21:58:57.251: ERROR/AndroidRuntime(346): Uncaught handler: thread main exiting due to uncaught exception
08-23 21:58:57.411: ERROR/AndroidRuntime(346): java.lang.NullPointerException
08-23 21:58:57.411: ERROR/AndroidRuntime(346):     at android.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:149)
08-23 21:58:57.411: ERROR/AndroidRuntime(346):     at adriaansapps.com.tasks.TaskListCursorAdapter.bindView(TaskListCursorAdapter.java:36)
08-23 21:58:57.411: ERROR/AndroidRuntime(346):     at adriaansapps.com.tasks.TaskListCursorAdapter.newView(TaskListCursorAdapter.java:83)
08-23 21:58:57.411: ERROR/AndroidRuntime(346):     at android.widget.CursorAdapter.getView(CursorAdapter.java:182)
08-23 21:58:57.411: ERROR/AndroidRuntime(346):     at android.widget.AbsListView.obtainView(AbsListView.java:1274)
08-23 21:58:57.411: ERROR/AndroidRuntime(346):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1147)
08-23 21:58:57.411: ERROR/AndroidRuntime(346):     at android.widget.ListView.onMeasure(ListView.java:1060)
08-23 21:58:57.411: ERROR/AndroidRuntime(346):     at android.view.View.measure(View.java:7964)
08-23 21:58:57.411: ERROR/AndroidRuntime(346):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
08-23 21:58:57.411: ERROR/AndroidRuntime(346):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888)
08-23 21:58:57.411: ERROR/AndroidRuntime(346):     at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:619)
08-23 21:58:57.411: ERROR/AndroidRuntime(346):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:280)
08-23 21:58:57.411: ERROR/AndroidRuntime(346):     at android.view.View.measure(View.java:7964)
08-23 21:58:57.411: ERROR/AndroidRuntime(346):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:569)
08-23 21:58:57.411: ERROR/AndroidRuntime(346):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:361)
08-23 21:58:57.411: ERROR/AndroidRuntime(346):     at android.view.View.measure(View.java:7964)
08-23 21:58:57.411: ERROR/AndroidRuntime(346):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
08-23 21:58:57.411: ERROR/AndroidRuntime(346):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-23 21:58:57.411: ERROR/AndroidRuntime(346):     at android.view.View.measure(View.java:7964)
08-23 21:58:57.411: ERROR/AndroidRuntime(346):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:464)
08-23 21:58:57.411: ERROR/AndroidRuntime(346):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
08-23 21:58:57.411: ERROR/AndroidRuntime(346):     at android.view.View.measure(View.java:7964)
08-23 21:58:57.411: ERROR/AndroidRuntime(346):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
08-23 21:58:57.411: ERROR/AndroidRuntime(346):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-23 21:58:57.411: ERROR/AndroidRuntime(346):     at android.view.View.measure(View.java:7964)
08-23 21:58:57.411: ERROR/AndroidRuntime(346):     at android.view.ViewRoot.performTraversals(ViewRoot.java:763)
08-23 21:58:57.411: ERROR/AndroidRuntime(346):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
08-23 21:58:57.411: ERROR/AndroidRuntime(346):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 21:58:57.411: ERROR/AndroidRuntime(346):     at android.os.Looper.loop(Looper.java:123)
08-23 21:58:57.411: ERROR/AndroidRuntime(346):     at android.app.ActivityThread.main(ActivityThread.java:4363)
08-23 21:58:57.411: ERROR/AndroidRuntime(346):     at java.lang.reflect.Method.invokeNative(Native Method)
08-23 21:58:57.411: ERROR/AndroidRuntime(346):     at java.lang.reflect.Method.invoke(Method.java:521)
08-23 21:58:57.411: ERROR/AndroidRuntime(346):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-23 21:58:57.411: ERROR/AndroidRuntime(346):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-23 21:58:57.411: ERROR/AndroidRuntime(346):     at dalvik.system.NativeStart.main(Native Method)

Curiously I have been happily programming and debugging away all day and out of the blue a long working and bug free portion of my app now throws up this error.

I suspect it might be a problem with the databases, but really not sure - nothing seems out of place there. Up until today I had a database with only one table - today I added a new table which also has a "_id" field as the name autoincrement key field, which I read in the docs was required for android list adapters (if memory serves). Having some experience in coding and some minor experience with databases, that seems like a bad idea to me but I am uncertain of if that is the problem and/or how else to do it.

The crash I am listing here does not display or make reference to the data in the other, new table. The cursor does not contain any query data from it. In fact if I rename the key field "_id" of the new table to something else (ie "_blah"), the crash remains.

Like I said, if I upload the apk to my phone, it works.

I have tried the following on the emulator: - restarting adb (and eclipse) - no result - uninstall from emulator - no result - destory AVD profile and recreate - no result

I am developing this under the Android 2.1 SDK, and have been for the last few weeks. I have only ever had that SDK installed, I am yet to update to 2.2.

Not sure what else I can say here - hoping somebody here has experience enough to shed some light on it.

Without a fix to this, I can anticipate a very slow development process here on out (assuming I have to upload to my phone to test new code changes...).

Sorry for being long winded - hoping I am providing enough info for one of you smarter people to nail this. Thanks


Solution

  • I think you should not call .bindView() directly form a source code. It seems you do it this way looking at your error dump:

    08-23 21:58:57.411: ERROR/AndroidRuntime(346):     at adriaansapps.com.tasks.TaskListCursorAdapter.bindView(TaskListCursorAdapter.java:36)
    08-23 21:58:57.411: ERROR/AndroidRuntime(346):     at adriaansapps.com.tasks.TaskListCursorAdapter.newView(TaskListCursorAdapter.java:83)
    

    Simply set an adapter of a ListView/GridView with ListView.setAdapter(...) and let the magic happen.