java.lang.RuntimeException:无法恢复活动

时间:2013-09-03 17:33:26

标签: android android-intent

我想要实现的是点击一个按钮,它应该打开相机..使用点击一下,然后它返回到应用程序,带有图片视图中点击图片的管道

package com.example.waterbill;

import java.io.ByteArrayOutputStream;
import java.io.File; 
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class Bill_meter_satus extends Activity {
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }

    Button b1, b2;
    EditText edt1;
    ImageView mImage;
    private static final int CAMERA_PIC_REQUEST = 1888;
    String values;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bill_meter_sat);
        b1 = (Button) findViewById(R.id.button1);
        b2 = (Button) findViewById(R.id.button2);
        edt1 = (EditText) findViewById(R.id.editText1);
        Bundle extra = getIntent().getExtras();

        if (extra != null) {
            values = extra.getString("Cons_Code");
            edt1.setText(values);
            Toast.makeText(Bill_meter_satus.this, values, Toast.LENGTH_SHORT)
                    .show();
        }

// onliclick按钮按下按钮

            b2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(
                        android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, CAMERA_PIC_REQUEST);

            }

        });
        b1.setOnClickListener(new View.OnClickListener() {
                     //on click of button move to next activity
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent next = new Intent(
                        "com.example.waterbill.Bill_meter_status_normal");
                next.putExtra("Cons_Code", values);
                startActivity(next);

            }
        });

    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_CANCELED) {
            if (requestCode == CAMERA_PIC_REQUEST) {
                // 2
                Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
                mImage.setImageBitmap(thumbnail);
                // 3
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
                // 4
                File file = new File(Environment.getExternalStorageDirectory()
                        + File.separator + "image.jpg");
                try {
                    file.createNewFile();
                    FileOutputStream fo = new FileOutputStream(file);
                    // 5
                    fo.write(bytes.toByteArray());
                    fo.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    }

}

logcat的

09-04 07:26:47.051: E/AndroidRuntime(12992): FATAL EXCEPTION: main
09-04 07:26:47.051: E/AndroidRuntime(12992): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=-1, data=Intent { act=inline-data dat=content://media/external/images/media/18169 typ=image/jpeg (has extras) }} to activity {com.example.waterbill/com.example.waterbill.Bill_meter_satus}: java.lang.NullPointerException
09-04 07:26:47.051: E/AndroidRuntime(12992):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3007)
09-04 07:26:47.051: E/AndroidRuntime(12992):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3050)
09-04 07:26:47.051: E/AndroidRuntime(12992):    at android.app.ActivityThread.access$1100(ActivityThread.java:127)
09-04 07:26:47.051: E/AndroidRuntime(12992):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1188)
09-04 07:26:47.051: E/AndroidRuntime(12992):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-04 07:26:47.051: E/AndroidRuntime(12992):    at android.os.Looper.loop(Looper.java:137)
09-04 07:26:47.051: E/AndroidRuntime(12992):    at android.app.ActivityThread.main(ActivityThread.java:4448)
09-04 07:26:47.051: E/AndroidRuntime(12992):    at java.lang.reflect.Method.invokeNative(Native Method)
09-04 07:26:47.051: E/AndroidRuntime(12992):    at java.lang.reflect.Method.invoke(Method.java:511)
09-04 07:26:47.051: E/AndroidRuntime(12992):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
09-04 07:26:47.051: E/AndroidRuntime(12992):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
09-04 07:26:47.051: E/AndroidRuntime(12992):    at dalvik.system.NativeStart.main(Native Method)
09-04 07:26:47.051: E/AndroidRuntime(12992): Caused by: java.lang.NullPointerException
09-04 07:26:47.051: E/AndroidRuntime(12992):    at com.example.waterbill.Bill_meter_satus.onActivityResult(Bill_meter_satus.java:88)
09-04 07:26:47.051: E/AndroidRuntime(12992):    at android.app.Activity.dispatchActivityResult(Activity.java:4649)
09-04 07:26:47.051: E/AndroidRuntime(12992):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3003)
09-04 07:26:47.051: E/AndroidRuntime(12992):    ... 11 more

1 个答案:

答案 0 :(得分:7)

正如LogCat所说:

07:26:47.051: E/AndroidRuntime(12992): Caused by: java.lang.NullPointerException 09-04

NullPointerException。我没有看到您初始化mImage。您必须使用onCreate()方法初始化它。

将此代码段添加到onCreate()方法。

mImage = (ImageView)findViewById(R.id.yourImageViewId);