使用contentprovideroperation获取nullPointerException

时间:2013-01-11 10:10:57

标签: android

我正在尝试通过代码更新android中的联系人照片。使用内容提供商操作我试图更新照片。更新代码片段中添加的查询。但没有任何反应。我的代码段在下面。

   @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 1 && data != null)

    {

        Uri contactData = data.getData();

        String[] projection = new String[] { Data.RAW_CONTACT_ID,
                Photo.PHOTO };

        Cursor c = getContentResolver().query(Data.CONTENT_URI, projection,
                null, null, null);

        if (c.moveToFirst()) {

            String dataid = c.getString(c
                    .getColumnIndex(Data.RAW_CONTACT_ID));
            String first = String.valueOf(dataid);

            byte[] dataid1 = c.getBlob(c.getColumnIndex(Photo.PHOTO));
            String first1 = String.valueOf(dataid1);
            System.out.println("fisttry");
            System.out.println(dataid);
            System.out.println(first);
            System.out.println(first1);
            System.out.println(dataid1);

            try {

                System.out.println("fisttry");
                ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

                ContentProviderOperation.Builder op = ContentProviderOperation
                        .newUpdate(ContactsContract.Data.CONTENT_URI);

                op.withSelection(
                        ContactsContract.Data.RAW_CONTACT_ID + "=?"
                                + " AND " + ContactsContract.Data.MIMETYPE
                                + "=?",
                        new String[] {
                                String.valueOf(dataid),
                                ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE });
                op.withValue(
                        ContactsContract.Data.MIMETYPE,
                        ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
                op.withValue(ContactsContract.CommonDataKinds.Photo.PHOTO,
                        photo);

                ops.add(op.build());
                System.out.println(op);
                System.out.println(ops);
            } catch (Exception e) {
                e.printStackTrace();
            }

            // Update

            try {
                this.getContentResolver().applyBatch(
                        ContactsContract.AUTHORITY, ops);
                System.out.println("secondtry");
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (OperationApplicationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        }
    }



}

Stacktrace:

01-12 01:19:22.202: E/AndroidRuntime(16057): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/0r69-303A36303A365A/71 (has extras) }} to activity {com.ileaf.camerafun/com.ileaf.camerafun.TrialActivity}: java.lang.NullPointerException
01-12 01:19:22.202: E/AndroidRuntime(16057):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3521)
01-12 01:19:22.202: E/AndroidRuntime(16057):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3563)
01-12 01:19:22.202: E/AndroidRuntime(16057):    at android.app.ActivityThread.access$2800(ActivityThread.java:126)
01-12 01:19:22.202: E/AndroidRuntime(16057):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2068)
01-12 01:19:22.202: E/AndroidRuntime(16057):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-12 01:19:22.202: E/AndroidRuntime(16057):    at android.os.Looper.loop(Looper.java:123)
01-12 01:19:22.202: E/AndroidRuntime(16057):    at android.app.ActivityThread.main(ActivityThread.java:4633)
01-12 01:19:22.202: E/AndroidRuntime(16057):    at java.lang.reflect.Method.invokeNative(Native Method)
01-12 01:19:22.202: E/AndroidRuntime(16057):    at java.lang.reflect.Method.invoke(Method.java:521)
01-12 01:19:22.202: E/AndroidRuntime(16057):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
01-12 01:19:22.202: E/AndroidRuntime(16057):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-12 01:19:22.202: E/AndroidRuntime(16057):    at dalvik.system.NativeStart.main(Native Method)
01-12 01:19:22.202: E/AndroidRuntime(16057): Caused by: java.lang.NullPointerException
01-12 01:19:22.202: E/AndroidRuntime(16057):    at android.content.ContentProviderProxy.applyBatch(ContentProviderNative.java:454)
01-12 01:19:22.202: E/AndroidRuntime(16057):    at android.content.ContentProviderClient.applyBatch(ContentProviderClient.java:95)
01-12 01:19:22.202: E/AndroidRuntime(16057):    at android.content.ContentResolver.applyBatch(ContentResolver.java:622)
01-12 01:19:22.202: E/AndroidRuntime(16057):    at com.ileaf.camerafun.TrialActivity.onActivityResult(TrialActivity.java:513)
01-12 01:19:22.202: E/AndroidRuntime(16057):    at android.app.Activity.dispatchActivityResult(Activity.java:3890)
01-12 01:19:22.202: E/AndroidRuntime(16057):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3517)
01-12 01:19:22.202: E/AndroidRuntime(16057):    ... 11 more

1 个答案:

答案 0 :(得分:0)

这是一个活动生命周期问题。当您调用startActivityForResult()时,启动活动会被暂停,如果Android缺少资源,则可以完全销毁。

从您开始的活动返回时,对onActivityResult()的调用发生在活动生命周期的早期 - 例如,片段中onActivityResult()内的'getActivity()'调用将返回null如果活动实际上已被销毁,但如果它没有被销毁则返回非空值。

因此,您无法在onActivityResult()方法中可靠地执行任何复杂操作。我在这里使用了两种模式。第一种方法是使用onActivityResult()将响应存储在实例变量中,然后在onResume()方法中执行操作;第二个是有条件地执行代码:

@Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    if (getActivity() != null) {
        // as we have an activity, it wasn't destroyed, and we can do stuff here
    }


}

您使用哪种解决方案取决于您正在处理的方案。当通过数据意图返回瞬态数据时,我使用第一个解决方案(因为这是我得到它的唯一方法),第二个解决方案是onActivityResult()的调用只是一个通知,我应该刷新视图数据存储在别处。