Question

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

Was it helpful?

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.

OTHER TIPS

Your problem is that you are not overriding the getViewTypeCount and getViewType methods. If you don't override these methods, sometimes Android will call your getView with a "convertView" parameter of incorrect type, if you pass this parameter to the adapter's getView you'll get a NullPointerException when it tries to reuse the object.

Why it works in the phone?. I'm not sure, maybe the SimpleCursorAdapter version in the phone checks that the "convertView" parameter you are passing is of the correct type. I found a case that crash the 2.1 emulator, but not the 1.5, 1.6, 2.2 or my phone.

If you want to try an easier way to solve the problem (but less optimal), you can always pass the "convertView" parameter to the SimpleCursorAdapter as "null". This way the SimpleCursorAdapter will never reuse any View, but it works in every place (emulator all versiones + phone).

You can check out this tutorial by Jeff Sharkey

Separating Lists with Headers in Android 0.9

Take a look a bit higher up the logcat logs. You might find there is an SQL error (in yellow normally) and it might say something about ambiguous column name. You might need to add the name of the table followed by a period to get the _id column.

I had this problem yesterday because of this problem.

Why it might not happen on your phone is because you might not have the new table created properly on the phone. Try clearing the data (using manage applications) and running your application again.

I'm 95% sure your problem is ambiguous column names, and you'll find it in the logcat just a little bit higher than the error you posted, maybe 3 lines above.

Looking at the Android 2.2 source code, the only ways I see SimpleCursorAdapter's bindView() possibly raising a NullPointerException is if the to array you supplied to the constructor was null or if the Cursor you supplied to the constructor was null.

Now, there are definitely some changes in SimpleCursorAdapter between 2.1 and 2.2, as line 149 of the 2.2 edition is not in bindView(). Hence, there may be some other possible case for a NullPointerException in 2.1 that I'm not seeing in 2.2.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top