views:

67

answers:

3

Hi all 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

A: 

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.

radek-k
The OP is chaining to the superclass' implementation of `bindView()`, which is perfectly reasonable.
CommonsWare
Hmmm - and the winner is radek-kBut why? I want an explanation please so I can learn. I did not think this was a problem because (a) it was in tutorials and (b) my app has been running fine with this for quite some time. But, for kicks I thought I would comment this out and presto, the app is now happily working. I have also tried to revert everything back to how I had it prior to implementing suggestions here and it still works with this line commented out. Answers? Justification? Please?
Adriaan
A: 

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.

matto1990
I didn't see logcat lines like that. I also tried putting in the table name followed by a full stop, no change. I do now see a few lines regarding refererences followed by what appears to be id codes. They don't exist in my R.id file though. Odd?
Adriaan
Can you post a bit more of the logcat above the error you already posted. Might help us out a little bit. Seems a very odd error
matto1990
Will do - when I get access to my code again. Unfortunately, coding is not my day job! :p
Adriaan
That's fine, it's not my day job either :)
matto1990
Ok here we go - I get a lot of these throughout the entire emulator boot up process when I hit the debug button (ie. emulator starting from scratch).Some of the hex number relate to some of my view components (none of those however that the custom list cursor adapter is looking for though) and some of them are not related to my program at all, but appear to be in the range of values for my items (ie in the 7f07, 7f05, 7f03, 7f06 ranges) `08-24 20:25:39.814: WARN/ResourceType(53): Resources don't contain package for resource number 0x7f0700e5`I cannot get formatting in commments?
Adriaan
Maybe edit your origional post. What I'm really interested in is the few lines above the error you already posted. The problem you're having sounds very much like one I had and the actual error message (which was causing a null cursor to be returned) was hidden just above the error message you already posted. I've had the "Resources don't contain package for resource number xxxxxxx" but I'm yet to find out what it means really. Never been a problem for me though.
matto1990
@matto1990 - I checked the logcat last night for any other database errors that you were talking about in your comments and could not find anything that remotely resembled a problem. I will post the lines above the error when I get home again tonight, but reasonably confident that the cursor is not null -- particularly when I commented out the `bindView` method call and the error disappears.... as per my accepted solution.
Adriaan
Hmmm fair enough. It's a bit of an odd one but if it fixes it and has no problems I'm sure it's fine.
matto1990
A: 

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.

CommonsWare
Sorry about code highlighting in this comment. Post on mobile is difficult at best. When in debug mode with breakpoints I can see that the cursor that I am passing in does not appear to be null, as in it appears to be attached to an sql query. What should I look for as a definitive way of telling if it is null? The to array is definitely not null. Would another possibility be the the R.id.* items are unresolvable? I have added a few new layouts over the last day and I don't think I have doubled up on id names but could that be doing it?
Adriaan