1
我想在從相機捕獲圖像以識別我捕獲的圖像後傳遞額外的意圖。對於我,從相機捕獲圖像後傳遞額外的意圖
int TAKE_PHOTO_CODE = 0;
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
在響應,
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TAKE_PHOTO_CODE && resultCode == EditProfileActivity.RESULT_OK) {
Log.d("CameraDemo", "Pic saved");
// Get passed intent here...
System.out.println("EXTRA INTENT: "+data.getStringExtra("profile_pic"));
}
}
上面的代碼工作正常。我在本地存儲中獲取圖像。但是,我想再傳遞一個字符串意圖。
喜歡的東西,
cameraIntent.putExtra("profile_pic", "true");
而且在響應方面我想這一意圖。但是,我得到nullpointer exception
。傳遞意圖的正確方法是什麼?
堆棧跟蹤
01-09 13:29:36.228 23214-23214/com.example.android.navigationdrawerexample E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.android.navigationdrawerexample, PID: 23214
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {com.example.android.navigationdrawerexample/com.example.android.mtesapp.EditProfileActivity}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3942)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3992)
at android.app.ActivityThread.access$1300(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1403)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5872)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.android.mtesapp.EditProfileActivity.onActivityResult(EditProfileActivity.java:316)
at android.app.Activity.dispatchActivityResult(Activity.java:5535)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3938)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3992)
at android.app.ActivityThread.access$1300(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1403)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5872)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
at dalvik.system.NativeStart.main(Native Method)
如果發生崩潰,請放置整個堆棧跟蹤。從logcat與您的文章。 – 2015-01-09 18:28:07
@GabeSechan:我已經添加了它。 – 2015-01-09 18:35:06
那你在第316行有一些空值。看看你的示例代碼 - 當開始意圖時,你正在創建一個本地的Intent變量。它不會在onActivityResult中訪問 - 你可能有一個範圍問題,你有兩個cameraIntent變量,而且全局從未設置?除此之外,我們需要更多onActivityResult的代碼。 – 2015-01-09 18:37:52