Getting nullPointerException with contentprovideroperation

I am trying to update a contact photo in android via code. Using Content Provider Service I tried to update the photo. Refresh the request added to the code snippet. But nothing happens. The following is a snippet of code.

@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 
0
source share
1 answer

This is an action lifecycle issue. When you call startActivityForResult() , the start action is paused and can, if Android lacks resources, be completely destroyed.

When you return from the action you started, the call to onActivityResult() occurs at an early stage of the activity life cycle - for example, calling the getActivity () function inside onActivityResult() in a fragment returns null if the action was actually destroyed, but returns non-zero if it was not destroyed.

Therefore, you cannot reliably do anything complicated with the onActivityResult() method. I have two templates that I use here. First, just use onActivityResult() to store the response in instance variables, and then do the action in the onResume() method; the second is to conditionally execute the code:

 @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 } } 

Which solution you use depends on the scenario you are dealing with. I use the first solution when the transient data is returned through the data intent (since this is the only way to get it), and the second when the onActivityResult() call is just a notification that I should update the view from the saved data elsewhere.

0
source

All Articles