在我的应用程序中,您点击一个启动图库的按钮。您选择一个图像,然后将该图像裁剪成一个正方形 (1:1)。根据该裁剪的结果,它会保存图像并将 uri 推送到新意图,该意图在 ImageView 中显示图像(非常简单)。不过,它大约有 30% 的时间有效,而且我在崩溃中找不到任何相似之处。似乎在我将它加载到我的设备上进行调试之后,它在我启动的前 2 次崩溃并在第 3 次运行。如果有人能帮助我理解这一点,我对此感到非常困惑,那就太好了!我的想法可能是 onResult 发生了很多事情,而我跳到另一个班级的意图在它完成其他任务之前就被解雇了。另外我是编码新手,所以请忍受我草率的代码。
日志猫:
09-10 11:12:55.291: E/AndroidRuntime(2608): FATAL EXCEPTION: main
09-10 11:12:55.291: E/AndroidRuntime(2608): java.lang.RuntimeException: Unable to resume activity {com.yourboytyler.shoutgram/com.yourboytyler.shoutgram.go}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=file:///mnt/sdcard/SHOUTgram/pic_1347297141912.jpg (has extras) }} to activity {com.yourboytyler.shoutgram/com.yourboytyler.shoutgram.go}: java.lang.NullPointerException
09-10 11:12:55.291: E/AndroidRuntime(2608): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2124)
09-10 11:12:55.291: E/AndroidRuntime(2608): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2139)
09-10 11:12:55.291: E/AndroidRuntime(2608): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1672)
09-10 11:12:55.291: E/AndroidRuntime(2608): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-10 11:12:55.291: E/AndroidRuntime(2608): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
09-10 11:12:55.291: E/AndroidRuntime(2608): at android.os.Handler.dispatchMessage(Handler.java:99)
09-10 11:12:55.291: E/AndroidRuntime(2608): at android.os.Looper.loop(Looper.java:130)
09-10 11:12:55.291: E/AndroidRuntime(2608): at android.app.ActivityThread.main(ActivityThread.java:3691)
09-10 11:12:55.291: E/AndroidRuntime(2608): at java.lang.reflect.Method.invokeNative(Native Method)
09-10 11:12:55.291: E/AndroidRuntime(2608): at java.lang.reflect.Method.invoke(Method.java:507)
09-10 11:12:55.291: E/AndroidRuntime(2608): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
09-10 11:12:55.291: E/AndroidRuntime(2608): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
09-10 11:12:55.291: E/AndroidRuntime(2608): at dalvik.system.NativeStart.main(Native Method)
09-10 11:12:55.291: E/AndroidRuntime(2608): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=file:///mnt/sdcard/SHOUTgram/pic_1347297141912.jpg (has extras) }} to activity {com.yourboytyler.shoutgram/com.yourboytyler.shoutgram.go}: java.lang.NullPointerException
09-10 11:12:55.291: E/AndroidRuntime(2608): at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
09-10 11:12:55.291: E/AndroidRuntime(2608): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2111)
09-10 11:12:55.291: E/AndroidRuntime(2608): ... 12 more
09-10 11:12:55.291: E/AndroidRuntime(2608): Caused by: java.lang.NullPointerException
09-10 11:12:55.291: E/AndroidRuntime(2608): at com.yourboytyler.shoutgram.go.onActivityResult(go.java:163)
09-10 11:12:55.291: E/AndroidRuntime(2608): at android.app.Activity.dispatchActivityResult(Activity.java:3934)
09-10 11:12:55.291: E/AndroidRuntime(2608): at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
09-10 11:12:55.291: E/AndroidRuntime(2608): ... 13 more
代码:
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
//Activity.RESULT_OK
if (resultCode == -1) {
Toast.makeText(getApplicationContext(), mUri.getPath(), //THIS IS LINE 163
Toast.LENGTH_SHORT).show();
b1.setEnabled(true);
ImageView imgView=(ImageView)findViewById(R.id.i1);
Uri imgUri=Uri.parse("file://" + mUri.getPath());
imgView.setImageURI(imgUri);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("uripic", mUri.toString());
editor.commit();
MediaScannerConnection.scanFile(this,
new String[] { goldburg.toString() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
Intent openConnectPoint2 = new Intent("com.my.pakage.NAME"); //changed for privacy
startActivity(openConnectPoint2);
}
if (resultCode == Activity.RESULT_CANCELED) {
b1.setEnabled(true);
return;
}
if (requestCode == CAMERA_RESULT) {
Intent intent = new Intent(this, CropImage.class);
// here you have to pass absolute path to your file
intent.putExtra("image-path", mUri.getPath());
intent.putExtra("scale", true);
startActivity(intent);
}
}