结果为空指针异常

时间:2012-09-10 17:50:04

标签: android nullpointerexception

在我的应用中,您点按了启动图库的按钮。您选择一个图像,然后将该图像裁剪为正方形(1:1)。在该裁剪的结果上,它保存图像并将uri推送到新的意图,该意图在ImageView中显示图像(非常简单)。它大约有30%的时间可以工作,我在崩溃中找不到任何相似之处。似乎在我将它加载到我的设备上进行调试之后,我发起的前2次崩溃并且第3次正常工作。我很困惑,如果有人能帮助我理解这将是伟大的!我的想法也许就是onResult发生了很多事情,我跳到另一个类的意图在它完成它的其他任务之前就被解雇了。此外,我是编码的新手,所以请耐心等待我的代码。

logcat的:

09-10 11:12:55.291: E/AndroidRuntime(2608): FATAL EXCEPTION: main
09-10 11:12:55.291: E/AndroidRuntime(2608): java.lang.RuntimeException: Unable to resume activity {com.yourboytyler.shoutgram/com.yourboytyler.shoutgram.go}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=file:///mnt/sdcard/SHOUTgram/pic_1347297141912.jpg (has extras) }} to activity {com.yourboytyler.shoutgram/com.yourboytyler.shoutgram.go}: java.lang.NullPointerException
09-10 11:12:55.291: E/AndroidRuntime(2608):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2124)
09-10 11:12:55.291: E/AndroidRuntime(2608):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2139)
09-10 11:12:55.291: E/AndroidRuntime(2608):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1672)
09-10 11:12:55.291: E/AndroidRuntime(2608):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-10 11:12:55.291: E/AndroidRuntime(2608):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
09-10 11:12:55.291: E/AndroidRuntime(2608):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-10 11:12:55.291: E/AndroidRuntime(2608):     at android.os.Looper.loop(Looper.java:130)
09-10 11:12:55.291: E/AndroidRuntime(2608):     at android.app.ActivityThread.main(ActivityThread.java:3691)
09-10 11:12:55.291: E/AndroidRuntime(2608):     at java.lang.reflect.Method.invokeNative(Native Method)
09-10 11:12:55.291: E/AndroidRuntime(2608):     at java.lang.reflect.Method.invoke(Method.java:507)
09-10 11:12:55.291: E/AndroidRuntime(2608):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
09-10 11:12:55.291: E/AndroidRuntime(2608):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
09-10 11:12:55.291: E/AndroidRuntime(2608):     at dalvik.system.NativeStart.main(Native Method)
09-10 11:12:55.291: E/AndroidRuntime(2608): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=file:///mnt/sdcard/SHOUTgram/pic_1347297141912.jpg (has extras) }} to activity {com.yourboytyler.shoutgram/com.yourboytyler.shoutgram.go}: java.lang.NullPointerException
09-10 11:12:55.291: E/AndroidRuntime(2608):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
09-10 11:12:55.291: E/AndroidRuntime(2608):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2111)
09-10 11:12:55.291: E/AndroidRuntime(2608):     ... 12 more
09-10 11:12:55.291: E/AndroidRuntime(2608): Caused by: java.lang.NullPointerException
09-10 11:12:55.291: E/AndroidRuntime(2608):     at com.yourboytyler.shoutgram.go.onActivityResult(go.java:163)
09-10 11:12:55.291: E/AndroidRuntime(2608):     at android.app.Activity.dispatchActivityResult(Activity.java:3934)
09-10 11:12:55.291: E/AndroidRuntime(2608):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
09-10 11:12:55.291: E/AndroidRuntime(2608):     ... 13 more

代码:

 protected void onActivityResult(int requestCode,  
            int resultCode, Intent data) { 
            //Activity.RESULT_OK
            if (resultCode == -1) {  
                Toast.makeText(getApplicationContext(), mUri.getPath(), //THIS IS LINE 163
                          Toast.LENGTH_SHORT).show();
                b1.setEnabled(true);
                ImageView imgView=(ImageView)findViewById(R.id.i1);
                Uri imgUri=Uri.parse("file://" + mUri.getPath());
                imgView.setImageURI(imgUri);

                SharedPreferences.Editor editor = prefs.edit();
                editor.putString("uripic", mUri.toString());
                editor.commit();


                MediaScannerConnection.scanFile(this,
                        new String[] { goldburg.toString() }, null,
                        new MediaScannerConnection.OnScanCompletedListener() {
                    public void onScanCompleted(String path, Uri uri) {
                        Log.i("ExternalStorage", "Scanned " + path + ":");
                        Log.i("ExternalStorage", "-> uri=" + uri);
                    }
               });




                Intent openConnectPoint2 = new Intent("com.my.pakage.NAME"); //changed for privacy
                startActivity(openConnectPoint2);  
            }
            if (resultCode == Activity.RESULT_CANCELED) {  
            b1.setEnabled(true);
                return; 
             }



            if (requestCode == CAMERA_RESULT) {  
            Intent intent = new Intent(this, CropImage.class);  
            // here you have to pass absolute path to your file  
            intent.putExtra("image-path", mUri.getPath());  
            intent.putExtra("scale", true);  
            startActivity(intent);  
        }  



        }  

1 个答案:

答案 0 :(得分:0)

从评论中提出

LogCat中最重要的信息是:

Caused by: java.lang.NullPointerException 
    at com.yourboytyler.shoutgram.go.onActivityResult(go.java:163)

你表示这是第163行:

Toast.makeText(getApplicationContext(), mUri.getPath(), Toast.LENGTH_SHORT).show();

我认为mUrinull,但正如您所说,只是有时候。当您尝试在.getPath()变量上调用方法(如null)时,会导致空指针异常。因此请仔细检查您设置mUri = ...

的位置