使用相机后,我在android应用程序中收到一个NullPointerException和Failure,从onActivityResult发送结果异常

使用相机后,我在android应用程序中收到一个NullPointerException和Failure,从onActivityResult发送结果异常,android,android-intent,nullpointerexception,android-camera,Android,Android Intent,Nullpointerexception,Android Camera,我已经看了很多其他的问题,但似乎没有一个对我有帮助。我正在写一个应用程序,你按下按钮打开相机。这很好,但一旦我去保存图片它崩溃,我得到的错误。 这是到目前为止我的代码 public class MainActivity extends Activity { private int TAKE_PICTURE = 1337; private Uri outputFileUri; private int screenHeight; private int sc

我已经看了很多其他的问题,但似乎没有一个对我有帮助。我正在写一个应用程序,你按下按钮打开相机。这很好,但一旦我去保存图片它崩溃,我得到的错误。 这是到目前为止我的代码

    public class MainActivity extends Activity {

    private int TAKE_PICTURE = 1337;
    private Uri outputFileUri;
    private int screenHeight;
    private int screenWidth;
    private ImageView background;
    private String ORIGINAL_FILE = "photo.jpg";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button take = (Button)findViewById(R.id.take);
        Button share = (Button)findViewById(R.id.share);
        take.setOnClickListener(new takeInstr(this));
        ImageView background = (ImageView)findViewById(R.id.background);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }


    public void startCamera() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File file = new File(Environment.getExternalStorageDirectory(), ORIGINAL_FILE);
        outputFileUri = Uri.fromFile(file);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        startActivityForResult(intent, TAKE_PICTURE);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == TAKE_PICTURE && resultCode != RESULT_CANCELED) {  
            if (data != null) {
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                String file = Environment.getExternalStorageDirectory() + "/test.jpg";
                BitmapFactory.decodeFile(file, options);
                int imageHeight = options.outHeight;
                int imageWidth = options.outWidth;
                String imageType = options.outMimeType;
                DisplayMetrics metrics = this.getResources().getDisplayMetrics();
                screenHeight = metrics.heightPixels;
                screenWidth = metrics.widthPixels;
                int heightRatio = Math.round((float) imageHeight / (float) screenHeight);
                int widthRatio = Math.round((float) imageWidth / (float) screenWidth);
                options.inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
                options.inJustDecodeBounds = false;
                background.setImageBitmap(BitmapFactory.decodeFile(file, options));
            }
            else {


            }
        }
    }
}

package com.example.photoshare;



import android.view.View;
import android.view.View.OnClickListener;

public class takeInstr implements OnClickListener {
    private MainActivity mainActivity;
    public takeInstr(MainActivity mainActivity) {
        this.mainActivity = mainActivity;
    }
    @Override
    public void onClick(View v) {
        mainActivity.startCamera();

    }

}

我知道当它返回null时,我应该做一些其他的事情,但我不知道具体该做什么。任何帮助都将不胜感激。

您的代码中有两种不同的东西可能是null。第一个问题可能是BitmapFactory无法解码您提供的文件。另外,您是否确定只想解码
test.jpg
,而不想解码摄像机拍摄的图像

第二个问题可能是您的
背景视图出现问题。根据活动的启动类型,可以在
onCreate()
之前调用
onActivityResult()
,这将导致
后台变量仍然为空


要深入挖掘,您应该将解码后的位图分配给一个新变量,查看它是否为null,并检查
background
是否为null…

代码中有两个不同的东西可能为null。第一个问题可能是BitmapFactory无法解码您提供的文件。另外,您是否确定只想解码
test.jpg
,而不想解码摄像机拍摄的图像

第二个问题可能是您的
背景视图出现问题。根据活动的启动类型,可以在
onCreate()
之前调用
onActivityResult()
,这将导致
后台变量仍然为空


为了更深入地挖掘,您应该将解码后的位图分配给一个新变量,查看它是否为null,并检查
background
是否为null…

原因:java.lang.ArrayIndexOutOfBoundsException:length=1;index=1位于com.example.photoshare.MainActivity.onActivityResult(MainActivity.java:75)
这不是NullPointerException。。。第75行是什么?你主要活动的第75行是什么?而且它不是一个空指针…听起来像是遇到了堆分配问题(就在崩溃之前Dalvik报告3%内存可用)。这阻止了Dalvik将图像正确地传送到你的应用程序。@Sam和may MainActivity的第75行只是我的else的结束括号,我不确定这是怎么造成的this@WarrenFaith嗯,我想我已经修复了空指针,未能交付与以前不同的内容,但我仍然丢失了
,原因是:java.lang.ArrayIndexOutOfBoundsException:长度=1;index=1位于com.example.photoshare.MainActivity.onActivityResult(MainActivity.java:75)
这不是NullPointerException。。。第75行是什么?你主要活动的第75行是什么?而且它不是一个空指针…听起来像是遇到了堆分配问题(就在崩溃之前Dalvik报告3%内存可用)。这阻止了Dalvik将图像正确地传送到你的应用程序。@Sam和may MainActivity的第75行只是我的else的结束括号,我不确定这是怎么造成的this@WarrenFaith嗯,我想我已经修复了空指针,但未能实现这一点与以前有所不同,但我仍然无法感谢您提供的所有帮助。我会试试看是否能解决问题。谢谢你的帮助。我将尝试一下,看看它是否能解决问题。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<ImageView 
    android:id="@+id/background"
    android:src="@drawable/great_wall"
    android:layout_width="fill_parent" 
    android:scaleType="fitXY"
    android:layout_height="fill_parent" />

<LinearLayout
    android:id="@+id/buttonLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:gravity="center_horizontal"
    android:weightSum="1.0" >

    <Button
        android:id="@+id/take"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:text="@string/take" />

    <Button
        android:id="@+id/share"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:text="@string/share" />
</LinearLayout>
02-18 12:42:56.266: W/dalvikvm(627): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
02-18 12:42:56.357: E/AndroidRuntime(627): FATAL EXCEPTION: main
02-18 12:42:56.357: E/AndroidRuntime(627): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1337, result=-1, data=null} to activity {com.example.photoshare/com.example.photoshare.MainActivity}: java.lang.NullPointerException
02-18 12:42:56.357: E/AndroidRuntime(627):  at android.app.ActivityThread.deliverResults(ActivityThread.java:2980)
02-18 12:42:56.357: E/AndroidRuntime(627):  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3023)
02-18 12:42:56.357: E/AndroidRuntime(627):  at android.app.ActivityThread.access$1100(ActivityThread.java:123)
02-18 12:42:56.357: E/AndroidRuntime(627):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1177)
02-18 12:42:56.357: E/AndroidRuntime(627):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 12:42:56.357: E/AndroidRuntime(627):  at android.os.Looper.loop(Looper.java:137)
02-18 12:42:56.357: E/AndroidRuntime(627):  at android.app.ActivityThread.main(ActivityThread.java:4424)
02-18 12:42:56.357: E/AndroidRuntime(627):  at java.lang.reflect.Method.invokeNative(Native Method)
02-18 12:42:56.357: E/AndroidRuntime(627):  at java.lang.reflect.Method.invoke(Method.java:511)
02-18 12:42:56.357: E/AndroidRuntime(627):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-18 12:42:56.357: E/AndroidRuntime(627):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-18 12:42:56.357: E/AndroidRuntime(627):  at dalvik.system.NativeStart.main(Native Method)
02-18 12:42:56.357: E/AndroidRuntime(627): Caused by: java.lang.NullPointerException
02-18 12:42:56.357: E/AndroidRuntime(627):  at com.example.photoshare.MainActivity.onActivityResult(MainActivity.java:69)
02-18 12:42:56.357: E/AndroidRuntime(627):  at android.app.Activity.dispatchActivityResult(Activity.java:4649)
02-18 12:42:56.357: E/AndroidRuntime(627):  at android.app.ActivityThread.deliverResults(ActivityThread.java:2976)
02-18 12:42:56.357: E/AndroidRuntime(627):  ... 11 more
02-18 12:42:56.686: I/dalvikvm(627): threadid=3: reacting to signal 3
02-18 12:42:56.726: I/dalvikvm(627): Wrote stack traces to '/data/anr/traces.txt'
02-18 12:42:57.047: I/dalvikvm(627): threadid=3: reacting to signal 3
02-18 12:42:57.086: I/dalvikvm(627): Wrote stack traces to '/data/anr/traces.txt'
02-18 12:42:59.716: I/Process(627): Sending signal. PID: 627 SIG: 9