Android

android.content.Intent.getParcelableExtra(java.lang.String)’ on a null object reference

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=777, result=0, data=null} to activity {com.test/com.test.SettingActivity}: java.lang.NullPointerException: Attempt to invoke virtual method ‘android.os.Parcelable android.content.Intent.getParcelableExtra(java.lang.String)’ on a null object reference
       at android.app.ActivityThread.deliverResults(ActivityThread.java:4434)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4476)
        at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6853)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:860)

     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method ‘android.os.Parcelable android.content.Intent.getParcelableExtra(java.lang.String)’ on a null object reference
        at com.test.SettingActivity.onActivityResult(Unknown Source:51)
        at android.app.Activity.dispatchActivityResult(Activity.java:7469)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4427)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4476) 
        at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6853) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:860) 

인텐트를 호출 후 결과를 돌려받을 때 발생한 오류이다. startActivityForResult(intent, 777);

오류가 발생한 코드

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 777) {
Uri pickedUri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
if(pickedUri != null){
String ringtonePath = pickedUri.toString();


SharedPreferences sp = getSharedPreferences(MY_PM_PREF, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString(POWER_OFF_RINGTONE,ringtonePath);
editor.commit();
ringtoneName.setText(getPowerOffRingToneName());
}
}

오류 수정 후 코드 

인텐트를 호출 후 결과를 돌려받을 때 발생한 오류이다.
리턴 받은 Intent 가 null이 아닌지 체크 후 가져오도록 처리하면 된다.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 777 && data!= null) {
Uri pickedUri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
if(pickedUri != null){
String ringtonePath = pickedUri.toString();


SharedPreferences sp = getSharedPreferences(MY_PM_PREF, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString(POWER_OFF_RINGTONE,ringtonePath);
editor.commit();
ringtoneName.setText(getPowerOffRingToneName());
}
}

Leave a Reply

error: Content is protected !!