在结果空指针异常 [英] Null Pointer Exception on result

查看:223
本文介绍了在结果空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,你你点击一个按钮,启动画廊。您选择一个图像,然后裁剪该图像成正方形(1:1)。在该作物的结果,这样可以节省的形象和推动URI到一个新的意图它显示在ImageView的(pretty简单)的图像。它虽然工作时间30%左右,我无法找到任何崩溃的相似之处。好像在我加载到我的设备调试崩溃的第2次我启动和作品的第三次。我对这个很困惑,如果有人能帮助我理解这一点,这将是伟大的!我的想法是,也许到多少是onResult发生的事情,我的意图跳转到另一个类炒鱿鱼之前,能够完成其任务等。此外,我是新来的编码,请与我马虎code承受的。

logcat的:

  11月9日日至10日:12:55.291:E / AndroidRuntime(2608):致命异常:主要
十一月9日至10日:12:55.291:E / AndroidRuntime(2608):了java.lang.RuntimeException:无法恢复活动{com.yourboytyler.shoutgram / com.yourboytyler.shoutgram.go}了java.lang.RuntimeException:传递失败结果ResultInfo {谁= NULL,请求= 1,结果= -1,数据= {意向A​​CT =文件:///mnt/sdcard/SHOUTgram/pic_1347297141912.jpg(有临时演员)}}到活动{com.yourboytyler.shoutgram /com.yourboytyler.shoutgram.go}:显示java.lang.NullPointerException
十一月9日至10日:12:55.291:E / AndroidRuntime(2608):在android.app.ActivityThread.performResumeActivity(ActivityThread.java:2124)
十一月9日至10日:12:55.291:E / AndroidRuntime(2608):在android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2139)
十一月9日至10日:12:55.291:E / AndroidRuntime(2608):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1672)
十一月9日至10日:12:55.291:E / AndroidRuntime(2608):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
十一月9日至10日:12:55.291:E / AndroidRuntime(2608):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:935)
十一月9日至10日:12:55.291:E / AndroidRuntime(2608):在android.os.Handler.dispatchMessage(Handler.java:99)
十一月9日至10日:12:55.291:E / AndroidRuntime(2608):在android.os.Looper.loop(Looper.java:130)
十一月9日至10日:12:55.291:E / AndroidRuntime(2608):在android.app.ActivityThread.main(ActivityThread.java:3691)
十一月9日至10日:12:55.291:E / AndroidRuntime(2608):在java.lang.reflect.Method.invokeNative(本机方法)
十一月9日至10日:12:55.291:E / AndroidRuntime(2608):在java.lang.reflect.Method.invoke(Method.java:507)
十一月9日至10日:12:55.291:E / AndroidRuntime(2608):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:907)
十一月9日至10日:12:55.291:E / AndroidRuntime(2608):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
十一月9日至10日:12:55.291:E / AndroidRuntime(2608):在dalvik.system.NativeStart.main(本机方法)
十一月9日至10日:12:55.291:E / AndroidRuntime(2608):了java.lang.RuntimeException:由故障造成的结果交付{ResultInfo谁= NULL,请求= 1,结果= -1,数据= {意向A​​CT =文件:///mnt/sdcard/SHOUTgram/pic_1347297141912.jpg(有临时演员)}}到活动{com.yourboytyler.shoutgram / com.yourboytyler.shoutgram.go}:显示java.lang.NullPointerException
十一月9日至10日:12:55.291:E / AndroidRuntime(2608):在android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
十一月9日至10日:12:55.291:E / AndroidRuntime(2608):在android.app.ActivityThread.performResumeActivity(ActivityThread.java:2111)
十一月9日至10日:12:55.291:E / AndroidRuntime(2608):12 ...更多
十一月9日至10日:12:55.291:E / AndroidRuntime(2608):因:显示java.lang.NullPointerException
十一月9日至10日:12:55.291:E / AndroidRuntime(2608):在com.yourboytyler.shoutgram.go.onActivityResult(go.java:163)
十一月9日至10日:12:55.291:E / AndroidRuntime(2608):在android.app.Activity.dispatchActivityResult(Activity.java:3934)
十一月9日至10日:12:55.291:E / AndroidRuntime(2608):在android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
十一月9日至10日:12:55.291:E / AndroidRuntime(2608):13 ...更多

code:

 保护无效的onActivityResult(INT申请code,
            INT结果code,意图数据){
            //Activity.RESULT_OK
            如果(结果code == -1){
                Toast.makeText(getApplicationContext(),mUri.getPath()//这是线163
                          Toast.LENGTH_SHORT).show();
                b1.setEnabled(真);
                ImageView的imgView =(ImageView的)findViewById(R.id.i1);
                乌里imgUri = Uri.parse(文件://+ mUri.getPath());
                imgView.setImageURI(imgUri);                共享preferences.Editor编辑器= prefs.edit();
                editor.putString(uripic,mUri.toString());
                editor.commit();
                MediaScannerConnection.scanFile(这一点,
                        新的String [] {goldburg.toString()},空,
                        新MediaScannerConnection.OnScanCompletedListener(){
                    公共无效onScanCompleted(字符串路径,开放的URI){
                        Log.i(ExternalStorage,扫描+路径+:);
                        Log.i(ExternalStorage, - > URI =+ URI);
                    }
               });
                意图openConnectPoint2 =新意图(com.my.pakage.NAME); //改变隐私
                startActivity(openConnectPoint2);
            }
            如果(结果code == Activity.RESULT_CANCELED){
            b1.setEnabled(真);
                返回;
             }            如果(要求code == CAMERA_RESULT){
            意向意图=新意图(这一点,CropImage.class);
            //这里你要通过绝对路径文件
            intent.putExtra(映像路径,mUri.getPath());
            intent.putExtra(规模化,真正的);
            startActivity(意向);
        }        }


解决方案

从意见长大

从LogCat中最重要的信息是:

 产生的原因:显示java.lang.NullPointerException
    在com.yourboytyler.shoutgram.go.onActivityResult(go.java:163)

和你表示这是行163:

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

我猜想,穆里,但正如你所说,只是有时。空指针异常引起的,当你试图调用一个方法,如 .getPath(),一个变量,。所以仔细检查你设置穆里= ...

In my app you you tap a button that launches the Gallery. You select an image and then crop that image into a square (1:1). on the result of that crop it saves the image and pushes the uri to a new intent which displays the image in an ImageView (pretty simple). It works about 30% of the time though and I can't find any similarities in crashes. It seems like after I load it onto my device to debug it crashes the first 2 times I launch and works the 3rd time. I'm very confused on this if anyone can help me understand this that would be great! My idea is maybe to much is happening onResult and my intent to jump to another class is getting fired before it can complete it's other task. Also I'm new to coding so please bear with my sloppy code.

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

Code:

 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);  
        }  



        }  

解决方案

Brought up from comments

The most important information from your LogCat is:

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

And you indicated that this is line 163:

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

I would guess that mUri is null, but as you stated, only sometimes. A null pointer exception is caused when you attempt to call a method, like .getPath(), on a variable that is null. So double check where you set mUri = ...

这篇关于在结果空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆