NPE on HTC Sensation, works in the emulator
I have used some code to create a simple app to allow the adding of contact details. The code is very simple and works fine in the emulator and on a number of devices that I have, except on the new HTC Sensation.
Here is the code:
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();
ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
.withValue(RawContacts.ACCOUNT_TYPE, null)
.withValue(RawContacts.ACCOUNT_NAME, null)
.build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CON开发者_开发百科TENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(Data.MIMETYPE,Phone.CONTENT_ITEM_TYPE)
.withValue(Phone.NUMBER, number.getText().toString().trim())
.withValue(Phone.TYPE, "TYPE_MOBILE")
.build());
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(Data.MIMETYPE,
StructuredName.CONTENT_ITEM_TYPE)
.withValue(StructuredName.DISPLAY_NAME, name.getText().toString().trim())
.build());
try {
ContentProviderResult[] res = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OperationApplicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
When I run it I get the following:
java.lang.NullPointerException
at android.os.Parcel.readException(Parcel.java:1328)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:160)
at android.database.DatabaseUtils.readExceptionWithOperationApplicationExceptionFromParcel(DatabaseUtils.java:137)
at android.content.ContentProviderProxy.applyBatch(ContentProviderNative.java:491)
at android.content.ContentProviderClient.applyBatch(ContentProviderClient.java:95)
at android.content.ContentResolver.applyBatch(ContentResolver.java:641)
at uk.co.androidfun.getthatnumber.mainActivity.saveCallContact(mainActivity.java:157)
at uk.co.androidfun.getthatnumber.mainActivity$3.onClick(mainActivity.java:74)
at android.view.View.performClick(View.java:2532)
at android.view.View$PerformClick.run(View.java:9277)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:4196)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
I have looked and looked at the code and cannot see an issue, and as I say works well in the emulator run 2.3.3 but not on the phone running the same.
Thanks John
I know this is an old question, but I was just hit by that weird NPE too, so this might help someone.
After some digging I found that one of the values I gave the newInsert
was of the wrong type (damn refactoring!)
I had:
String val = ...;
builder.withValue(Data.RAW_CONTACT_ID, val);
Instead of:
long val = ...;
builder.withValue(Data.RAW_CONTACT_ID, val);
Fixing that solved the NPE.
So, looking at your code, I would first change "TYPE_MOBILE" to the Android const CommonDataKinds.Phone.TYPE_MOBILE
(since this value might change among different vendors),
Second, make sure all the keys you're using in withValue
have the same type as the one you're giving them (not sure 'null' is legal for ACCOUNT_TYPE
), and print trimmed strings to make sure they have meaningful values.
Hope this helps!
Check what code refers to line of
androidfun.getthatnumber.mainActivity.saveCallContact(mainActivity.java:157)
There also should be more to your logcat, a Caused by
etc etc that is usually more direct in pointing to the bad code.
I had the same problem with two HTC devices. What solved it for me is adding some more values to the last data entry (I've added five more, other then the raw contact ID, phone number and mime tyep). I have no idea why it works now, but it does.
精彩评论