当调用startActivityForResult来捕获图像时,onActivityResult返回NullPointerException

时间:2014-04-03 04:02:18

标签: android android-intent

我正在使用startActivityForResult调用相机应用程序并自行传递图像URI。但是在捕获图像后,我会在NullpointerException中收到onActivityResult

生成的堆栈是:

E AndroidRuntime: Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.example.fieldexecutive/com.example.fieldexecutive.MainActivity}: java.lang.NullPointerException    
E AndroidRuntime:   at android.app.ActivityThread.deliverResults(ActivityThread.java:2997)    
E AndroidRuntime:   at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2445)    
E AndroidRuntime:   ... 13 more    
E AndroidRuntime: Caused by: java.lang.NullPointerException    
E AndroidRuntime:   at com.example.fieldexecutive.MainActivity.onActivityResult(MainActivity.java:152)    
E AndroidRuntime:   at android.app.Activity.dispatchActivityResult(Activity.java:4654)    
E AndroidRuntime:   at android.app.ActivityThread.deliverResults(ActivityThread.java:2993)    
E AndroidRuntime:   ... 14 more

在我的onActivityResult我甚至没有使用返回的数据,而是我使用传递的URI。此外,图像将保存在传递的URI中,但数据将返回null

有时会出现此问题。大概五次我运行我的应用程序。

以下是代码:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); 
this.startActivityForResult(intent, RES_IMAGE_CAPTURE_CHEAQUE);

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

     switch (requestCode) { 

            case RES_IMAGE_CAPTURE_CHEAQUE: 

                Log.e( TAG, "resultCode: " + resultCode );

                switch( resultCode )
                {
                    case 0:
                        Log.e( TAG, "in onActivityResult : image cancelled" );
                        break;
                    case -1:
                        //image storead, now load it in the web
                        Intent i=new Intent(this,CheaqueActivity.class);

                        startActivity(i);
                        break;
                    }
                break;
        }
    }

0 个答案:

没有答案