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

时间:2022-12-15 18:53:56

What I am trying to achieve is on click of a button it should open the camera ..the use clicks a snap and it returns back to the app with tubnail of the clicked pic in the image view

我正在努力实现的是点击一个按钮就可以打开相机。使用点击一个snap,它返回到应用程序,在图像视图中点击图片的tubnail。

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 of the button caputre a snap

//按一下按钮就可以了。

            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

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 个解决方案

#1


5  

As LogCat said:

作为LogCat说:

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

It is NullPointerException. I don't see that you initialize your mImage. You must initialize it in onCreate() method.

这是NullPointerException。我没看到你初始化了你的mImage。您必须在onCreate()方法中初始化它。

Add this code snippet to onCreate() method.

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

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

#1


5  

As LogCat said:

作为LogCat说:

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

It is NullPointerException. I don't see that you initialize your mImage. You must initialize it in onCreate() method.

这是NullPointerException。我没看到你初始化了你的mImage。您必须在onCreate()方法中初始化它。

Add this code snippet to onCreate() method.

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

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