无法以随机顺序恢复活动
Posted
技术标签:
【中文标题】无法以随机顺序恢复活动【英文标题】:Unable to resume activity in random order 【发布时间】:2012-12-19 17:07:36 【问题描述】:android 平台 4.0.4 Opencv 2.4.3
我正在尝试从相机获取快照,然后使用 opencv 处理它! 但有时(以随机顺序)我收到以下异常
12-19 18:54:33.659: E/AndroidRuntime(16625): FATAL EXCEPTION: main
12-19 18:54:33.659: E/AndroidRuntime(16625): java.lang.RuntimeException: Unable to resume activity com.example.superemr/com.example.superemr.MainActivity: java.lang.RuntimeException: Failure delivering result ResultInfowho=null, request=11, result=-1, data=Intent act=inline-data dat=file:///mnt/sdcard/Pictures/IDSMART/IDS-1607435583.jpg typ=image/jpeg (has extras) to activity com.example.superemr/com.example.superemr.MainActivity: java.lang.NullPointerException
12-19 18:54:33.659: E/AndroidRuntime(16625): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2464)
12-19 18:54:33.659: E/AndroidRuntime(16625): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2492)
12-19 18:54:33.659: E/AndroidRuntime(16625): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1997)
12-19 18:54:33.659: E/AndroidRuntime(16625): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3371)
12-19 18:54:33.659: E/AndroidRuntime(16625): at android.app.ActivityThread.access$700(ActivityThread.java:127)
12-19 18:54:33.659: E/AndroidRuntime(16625): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1162)
12-19 18:54:33.659: E/AndroidRuntime(16625): at android.os.Handler.dispatchMessage(Handler.java:99)
12-19 18:54:33.659: E/AndroidRuntime(16625): at android.os.Looper.loop(Looper.java:137)
12-19 18:54:33.659: E/AndroidRuntime(16625): at android.app.ActivityThread.main(ActivityThread.java:4441)
12-19 18:54:33.659: E/AndroidRuntime(16625): at java.lang.reflect.Method.invokeNative(Native Method)
12-19 18:54:33.659: E/AndroidRuntime(16625): at java.lang.reflect.Method.invoke(Method.java:511)
12-19 18:54:33.659: E/AndroidRuntime(16625): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-19 18:54:33.659: E/AndroidRuntime(16625): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-19 18:54:33.659: E/AndroidRuntime(16625): at dalvik.system.NativeStart.main(Native Method)
12-19 18:54:33.659: E/AndroidRuntime(16625): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfowho=null, request=11, result=-1, data=Intent act=inline-data dat=file:///mnt/sdcard/Pictures/IDSMART/IDS-1607435583.jpg typ=image/jpeg (has extras) to activity com.example.superemr/com.example.superemr.MainActivity: java.lang.NullPointerException
12-19 18:54:33.659: E/AndroidRuntime(16625): at android.app.ActivityThread.deliverResults(ActivityThread.java:3000)
12-19 18:54:33.659: E/AndroidRuntime(16625): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2451)
12-19 18:54:33.659: E/AndroidRuntime(16625): ... 13 more
12-19 18:54:33.659: E/AndroidRuntime(16625): Caused by: java.lang.NullPointerException
12-19 18:54:33.659: E/AndroidRuntime(16625): at com.example.superemr.MainActivity.onActivityResult(MainActivity.java:88)
12-19 18:54:33.659: E/AndroidRuntime(16625): at android.app.Activity.dispatchActivityResult(Activity.java:4649)
12-19 18:54:33.659: E/AndroidRuntime(16625): at android.app.ActivityThread.deliverResults(ActivityThread.java:2996)
12-19 18:54:33.659: E/AndroidRuntime(16625): ... 14 more
当我尝试调试问题时,通常程序会成功,但有时会在调用 onResume 之前抛出异常!
public void onResume()
super.onResume();
if(!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback))
Toast.makeText(this, "cannot connect to opencv ",Toast.LENGTH_SHORT).show();
public void cap(View view)
Intent camInt = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
List<ResolveInfo> aa = getPackageManager().queryIntentActivities(
camInt, PackageManager.MATCH_DEFAULT_ONLY);
if (aa != null && aa.size() > 0)
File dir = new File(
android.os.Environment
.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_PICTURES),
"IDSMART");
if (!dir.exists())
boolean result = dir.mkdirs();
Log.d("Creating Dirs Result:",
result ? "Create have been Done :)"
: "Create have not been done :(");
Random r = new Random();
file = new File(dir, String.format("IDS%s.jpg", r.nextInt()));
camInt.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(camInt, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
【问题讨论】:
问题出在你的 onActivityResult:at com.example.superemr.MainActivity.onActivityResult(MainActivity.java:88)
,只检查空值。
如果您需要帮助,请发布您的 onActivityResult
代码。
【参考方案1】:
在您的onActivityResult
中,请检查数据(Intent)是否为空。
如果您在尝试访问数据时收到 NullPointerException
。
尝试遵循 if 条件。
void onActivityResult(int requestCode, int resultCode, Intent data)
if( (resultCode == Activity.RESULT_OK) && (data != null))
// Do whatever you want here.
【讨论】:
以上是关于无法以随机顺序恢复活动的主要内容,如果未能解决你的问题,请参考以下文章