尝试拍摄第二张照片时应用崩溃

时间:2015-12-18 08:55:37

标签: android android-intent android-camera

我可以拍照并以新的布局显示。但当我回到我之前的布局并尝试拍摄新照片时,我的应用程序崩溃说'相机已停止'

我是android的初学者。谁能帮我?

编辑:我的应用程序崩溃,因为我的XML中有一个onclick

这是我的崩溃报告:

12-18 10:06:36.000 16690-16690/groep4.multec.integration.ehb.com.camera E/AndroidRuntime: FATAL EXCEPTION: main
12-18 10:06:36.000 16690-16690/groep4.multec.integration.ehb.com.camera E/AndroidRuntime: Process: groep4.multec.integration.ehb.com.camera, PID: 16690
12-18 10:06:36.000 16690-16690/groep4.multec.integration.ehb.com.camera E/AndroidRuntime: java.lang.IllegalStateException: Could not find method sendMessage(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.widget.ImageButton with id 'buttonFoto'
12-18 10:06:36.000 16690-16690/groep4.multec.integration.ehb.com.camera E/AndroidRuntime:     at android.view.View$DeclaredOnClickListener.resolveMethod(View.java:4485)
12-18 10:06:36.000 16690-16690/groep4.multec.integration.ehb.com.camera E/AndroidRuntime:     at android.view.View$DeclaredOnClickListener.onClick(View.java:4449)
12-18 10:06:36.000 16690-16690/groep4.multec.integration.ehb.com.camera E/AndroidRuntime:     at android.view.View.performClick(View.java:5204)
12-18 10:06:36.000 16690-16690/groep4.multec.integration.ehb.com.camera E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:21153)
12-18 10:06:36.000 16690-16690/groep4.multec.integration.ehb.com.camera E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
12-18 10:06:36.000 16690-16690/groep4.multec.integration.ehb.com.camera E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
12-18 10:06:36.000 16690-16690/groep4.multec.integration.ehb.com.camera E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148)
12-18 10:06:36.000 16690-16690/groep4.multec.integration.ehb.com.camera E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417)
12-18 10:06:36.000 16690-16690/groep4.multec.integration.ehb.com.camera E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
12-18 10:06:36.000 16690-16690/groep4.multec.integration.ehb.com.camera E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
12-18 10:06:36.000 16690-16690/groep4.multec.integration.ehb.com.camera E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

这是我的代码:

import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    ImageButton btnTakePhoto;
    ImageView imgTakenPhoto;
    private static final int CAM_REQUEST = 1313;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnTakePhoto = (ImageButton) findViewById(R.id.buttonFoto);
        imgTakenPhoto = (ImageView) findViewById(R.id.genomenFoto);


    btnTakePhoto.setOnClickListener(new btnTakePhotoClicker());
}
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == CAM_REQUEST){
            setContentView(R.layout.share);
            Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
            imgTakenPhoto = (ImageView) findViewById(R.id.genomenFoto);
            imgTakenPhoto.setImageBitmap(thumbnail);
        }
    }

    class btnTakePhotoClicker implements Button.OnClickListener {

        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAM_REQUEST);
        }
    }
    //Share layout -- back button - Go back to main layout
    public void ibBackToPhotograph(View v) {
        setContentView(R.layout.activity_main);
    }
}

2 个答案:

答案 0 :(得分:1)

试试这个

number_format

if(resultCode == RESULT_OK && requestCode == CAM_REQUEST){
        setContentView(R.layout.share);
        Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
        //imgTakenPhoto = (ImageView) findViewById(R.id.genomenFoto);
        imgTakenPhoto.setImageBitmap(thumbnail);
    }

答案 1 :(得分:0)

在你的班级ibBackToPhotograph功能

setContentView(R.layout.activity_main) 

用新视图替换旧视图。 我想要与新UI进行交互,您需要再次声明每个小部件,如下所示

public void ibBackToPhotograph(View v) {
    setContentView(R.layout.activity_main);
    btnTakePhoto = (ImageButton) findViewById(R.id.buttonFoto);
    imgTakenPhoto = (ImageView) findViewById(R.id.genomenFoto);
    btnTakePhoto.setOnClickListener(new btnTakePhotoClicker());
}