gpt4 book ai didi

android - 无法以随机顺序恢复 Activity

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:28:10 27 4
gpt4 key购买 nike

Android 平台 4.0.4Opencv 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 ResultInfo{who=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 ResultInfo{who=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中,请检查数据(Intent)是否为空。
如果在尝试访问数据时出现 NullPointerException
尝试以下 if 条件。

void onActivityResult(int requestCode, int resultCode, Intent data) 
{

if( (resultCode == Activity.RESULT_OK) && (data != null))
{
// Do whatever you want here.

}
}

关于android - 无法以随机顺序恢复 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13957501/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com