【发布时间】:2011-06-20 15:02:51
【问题描述】:
我使用了一些代码来创建一个简单的应用程序,以允许添加联系方式。代码非常简单,在模拟器和我拥有的许多设备上都可以正常工作,新的 HTC Sensation 除外。
代码如下:
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.CONTENT_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();
}
当我运行它时,我得到以下信息:
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)
我查看并查看了代码,没有发现问题,正如我所说,在运行 2.3.3 的模拟器中运行良好,但在运行相同的手机上却不行。
谢谢 约翰
【问题讨论】:
-
Parcel.java 中的第 1328 行是哪一行?
标签: java android nullpointerexception