首先,我知道这是一个经常发生的错误,并且有很多关于这个错误的问题。问题是它只在完全关闭并重新启动后发生一次,并且调试器不允许我检查正在发生的事情。
这是启动 Activity 的代码
protected File mediaTemp = null;
private void addPictures() {
try {
// On prends et enregistre la photo
MediafileHelper mediaHelper = new MediafileHelper(this);
mediaTemp = mediaHelper
.createMediaFile(MediafileHelper.PICTURE_FILE_EXT);
Intent takePictureIntent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(mediaTemp));
startActivityForResult(takePictureIntent,
MediafileHelper.PICTURE_ACTION_CODE);
} catch (IOException e) {
e.printStackTrace();
}
}
和 onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
int i = 1;
switch (requestCode) {
case MediafileHelper.PICTURE_ACTION_CODE:
/*Line 240*/ if (resultCode == RESULT_OK && mediaTemp.exists()) {
// Ajout de l'image à la collection.
Mediafile mediaFile = new Mediafile(
Mediafile.TYPE_PICTURE_JPG, mediaTemp);
mediaFile.setCaptureDatetime(new Date());
mediaFile.setLocation(REAC.getLocationHelper()
.getCurrentLocation());
medias.add(mediaFile);
addPictures();
} else {
if (resultCode == RESULT_OK && !mediaTemp.exists()) {
// Le fichier n'a pas pu être créé pour une raison hors
// de notre contrôle. (Appel téléphonique ou autre)
Toast.makeText(this,
getString(R.string.mediafile_file_not_created),
Toast.LENGTH_LONG).show();
}
// On rafraichis la liste des médias.
mediasFragment.getMediafileAdapter().notifyDataSetChanged();
}
break;
case MediafileHelper.VIDEO_ACTION_CODE:
...
}
}
最后,堆栈跟踪
09-27 10:01:09.211 E/AndroidRuntime( 4094): FATAL EXCEPTION: main
09-27 10:01:09.211 E/AndroidRuntime( 4094): java.lang.RuntimeException: Unable to resume activity {com.teops.qsl.reac/com.teops.qsl.reac.PicturesActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=742, result=-1, data=null} to activity {com.teops.qsl.reac/com.teops.qsl.reac.PicturesActivity}: java.lang.NullPointerException
09-27 10:01:09.211 E/AndroidRuntime( 4094): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2642)
09-27 10:01:09.211 E/AndroidRuntime( 4094): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2670)
09-27 10:01:09.211 E/AndroidRuntime( 4094): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2140)
09-27 10:01:09.211 E/AndroidRuntime( 4094): at android.app.ActivityThread.access$700(ActivityThread.java:143)
09-27 10:01:09.211 E/AndroidRuntime( 4094): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
09-27 10:01:09.211 E/AndroidRuntime( 4094): at android.os.Handler.dispatchMessage(Handler.java:99)
09-27 10:01:09.211 E/AndroidRuntime( 4094): at android.os.Looper.loop(Looper.java:137)
09-27 10:01:09.211 E/AndroidRuntime( 4094): at android.app.ActivityThread.main(ActivityThread.java:4967)
09-27 10:01:09.211 E/AndroidRuntime( 4094): at java.lang.reflect.Method.invokeNative(Native Method)
09-27 10:01:09.211 E/AndroidRuntime( 4094): at java.lang.reflect.Method.invoke(Method.java:511)
09-27 10:01:09.211 E/AndroidRuntime( 4094): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1011)
09-27 10:01:09.211 E/AndroidRuntime( 4094): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
09-27 10:01:09.211 E/AndroidRuntime( 4094): at dalvik.system.NativeStart.main(Native Method)
09-27 10:01:09.211 E/AndroidRuntime( 4094): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=742, result=-1, data=null} to activity {com.teops.qsl.reac/com.teops.qsl.reac.PicturesActivity}: java.lang.NullPointerException
09-27 10:01:09.211 E/AndroidRuntime( 4094): at android.app.ActivityThread.deliverResults(ActivityThread.java:3208)
09-27 10:01:09.211 E/AndroidRuntime( 4094): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2629)
09-27 10:01:09.211 E/AndroidRuntime( 4094): ... 12 more
09-27 10:01:09.211 E/AndroidRuntime( 4094): Caused by: java.lang.NullPointerException
09-27 10:01:09.211 E/AndroidRuntime( 4094): at com.teops.qsl.reac.mediafile.MediafileActivity.onActivityResult(MediafileActivity.java:240)
09-27 10:01:09.211 E/AndroidRuntime( 4094): at android.app.Activity.dispatchActivityResult(Activity.java:5344)
09-27 10:01:09.211 E/AndroidRuntime( 4094): at android.app.ActivityThread.deliverResults(ActivityThread.java:3204)
09-27 10:01:09.211 E/AndroidRuntime( 4094): ... 13 more
09-27 10:01:26.660 D/ActivityThread( 4363): setTargetHeapUtilization:0.25
09-27 10:01:26.660 D/ActivityThread( 4363): setTargetHeapIdealFree:8388608
09-27 10:01:26.660 D/ActivityThread( 4363): setTargetHeapConcurrentStart:2097152
09-27 10:01:26.740 D/LocationHelper( 4363): Location : Location[mProvider=network,mTime=1380290486532,mLatitude=46.7949937,mLongitude=-71.2240393,mHasAltitude=false,mAltitude=0.0,mHasSpeed=false,mSpeed=0.0,mHasBearing=false,mBearing=0.0,mHasAccuracy=true,mAccuracy=20.0,mExtras=Bundle[mParcelledData.dataSize=148]]
09-27 10:01:27.010 I/Adreno200-EGLSUB( 4363): <ConfigWindowMatch:2136>: Format RGBA_8888.
09-27 10:01:27.020 E/ ( 4363): <s3dReadConfigFile:75>: Can't open file for reading
09-27 10:01:27.020 E/ ( 4363): <s3dReadConfigFile:75>: Can't open file for reading
所以有人会告诉我那里有问题
09-27 10:01:09.211 E/AndroidRuntime( 4094): Caused by: java.lang.NullPointerException
09-27 10:01:09.211 E/AndroidRuntime( 4094): at com.teops.qsl.reac.mediafile.MediafileActivity.onActivityResult(MediafileActivity.java:240)
我在(第 238 行)放置了一个断点,int i = 1;
以便能够调试接下来的几行,但调试器不会停在那里,我的应用程序因该日志而崩溃。
我try{} catch(Exception e){ e.printStackTrace();}
在CASE的内容周围放置了一个。它不会使应用程序崩溃,而是不会继续拍照,而是返回到 Activity,不会将图片添加到集合中,也不会打印 StackTrace。一个刹车点e.printStackTrace()
不刹车。
try {
// Résultat de "Prise de photo"
if (resultCode == RESULT_OK && mediaTemp.exists()) {
// Ajout de l'image à la collection.
Mediafile mediaFile = new Mediafile(
Mediafile.TYPE_PICTURE_JPG, mediaTemp);
mediaFile.setCaptureDatetime(new Date());
mediaFile.setLocation(REAC.getLocationHelper()
.getCurrentLocation());
medias.add(mediaFile);
addPictures();
} else {
if (resultCode == RESULT_OK && !mediaTemp.exists()) {
// Le fichier n'a pas pu être créé pour une raison
// hors
// de notre contrôle. (Appel téléphonique ou autre)
Toast.makeText(
this,
getString(R.string.mediafile_file_not_created),
Toast.LENGTH_LONG).show();
}
// On rafraichis la liste des médias.
mediasFragment.getMediafileAdapter()
.notifyDataSetChanged();
}
} catch (Exception e) {
e.printStackTrace();
}
在任何情况下,图片都保存在由mediaTemp
.
请帮忙!