5

安卓平台 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 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);
      }
}
4

1 回答 1

0

在您的onActivityResult中,请检查数据(意图)是否为空。如果您在尝试访问数据时
得到,。 尝试遵循 if 条件。NullPointerException

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

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

      }
}
于 2013-01-02T16:28:03.543 回答