应用程序不断崩溃(相机)

时间:2015-05-06 05:28:16

标签: android

我正在制作一个应用程序,您可以在其中显示照片并显示它以及更改背景图像的外观。我的应用程序一直在崩溃,并想知道你们是否知道这个问题是什么。干杯

MainActivity:

package com.example.triptych4;

import java.io.File;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

    // label our logs "CameraApp3"
    private static String logtag = "CameraApp3";
    // tells us which camera to take a picture from
    private static int TAKE_PICTURE = 1;
    // empty variable to hold our image Uri once we store it
    private Uri imageUri;

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

        // look for the button we set in the view
        Button cameraButton = (Button)
                findViewById(R.id.button_camera);
        // set a listener on the button
        cameraButton.setOnClickListener(cameraListener);


    }

    // set a new listener
    private OnClickListener cameraListener = new OnClickListener() {
        public void onClick(View v) {
            // open the camera and pass in the current view
            takePhoto(v);
        }
    };

    public void takePhoto(View v) {
        // tell the phone we want to use the camera
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        // create a new temp file called pic.jpg in the "pictures" storage area of the phone
        File photo = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "pic.jpg");
        // take the return data and store it in the temp file "pic.jpg"
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
        // stor the temp photo uri so we can find it later
        imageUri = Uri.fromFile(photo);
        // start the camera
        startActivityForResult(intent, TAKE_PICTURE);

    }

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

    // override the original activity result function
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // call the parent
        super.onActivityResult(requestCode, resultCode, data);
        switch(requestCode) {
        // if the requestCode was equal to our camera code (1) then...
        case 1:
            // if the user took a photo and selected the photo to use
            if(resultCode == Activity.RESULT_OK) {
                // get the image uri from earlier
                Uri selectedImage = imageUri;
                // notify any apps of any changes we make
                getContentResolver().notifyChange(selectedImage, null);
                // get the imageView we set in our view earlier
                ImageButton imageButton = (ImageButton)findViewById(R.id.button_camera);
                // create a content resolver object which will allow us to access the image file at the uri above
                ContentResolver cr = getContentResolver();
                // create an empty bitmap object
                Bitmap bitmap;
                try {
                    // get the bitmap from the image uri using the content resolver api to get the image
                    bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, selectedImage);
                    // set the bitmap to the image view
                    imageButton.setImageBitmap(bitmap);
                    // notify the user
                    Toast.makeText(MainActivity.this, selectedImage.toString(), Toast.LENGTH_LONG).show();
                } catch(Exception e) {
                    // notify the user
                    Toast.makeText(MainActivity.this, "failed to load", Toast.LENGTH_LONG).show();
                    Log.e(logtag, e.toString());
                }
            }
        }
    }

}

MyAndroidAppActivity:

package com.example.triptych4;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.view.View;
import android.view.View.OnClickListener;

public class MyAndroidAppActivity extends Activity {

    Button button;
    ImageView image;

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

        addListenerOnButton();

    }

    public void addListenerOnButton() {

        image = (ImageView) findViewById(R.id.imageView1);

        button = (Button) findViewById(R.id.btnChangeImage);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                image.setImageResource(R.drawable.android3d);


            }


        });

    }



}

布局          

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent" >
    </FrameLayout>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="390dp"
        android:layout_marginTop="-35dp"
        android:src="@drawable/android" />

    <ImageButton
        android:id="@+id/button_camera"
        android:layout_width="170dp"
        android:layout_height="258dp"
        android:layout_marginLeft="-352dp"
        android:layout_marginTop="25dp"
        android:background="@drawable/middle" />

            <Button
                android:id="@+id/btnChangeImage"
                android:layout_width="160dp"
                android:layout_height="34dp"
                android:layout_marginLeft="-167dp"
                android:text="Change Image" />



</LinearLayout>

清单:          

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" 
            android:screenOrientation="landscape">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

logcat的:

05-06 14:41:19.363: I/art(1335): Background sticky concurrent mark sweep GC freed 1722(89KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 2MB/2MB, paused 1.335ms total 105.604ms
05-06 14:41:19.650: D/AndroidRuntime(1335): Shutting down VM
05-06 14:41:19.659: E/AndroidRuntime(1335): FATAL EXCEPTION: main
05-06 14:41:19.659: E/AndroidRuntime(1335): Process: com.example.triptych4, PID: 1335
05-06 14:41:19.659: E/AndroidRuntime(1335): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.triptych4/com.example.triptych4.MainActivity}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
05-06 14:41:19.659: E/AndroidRuntime(1335):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
05-06 14:41:19.659: E/AndroidRuntime(1335):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
05-06 14:41:19.659: E/AndroidRuntime(1335):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
05-06 14:41:19.659: E/AndroidRuntime(1335):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
05-06 14:41:19.659: E/AndroidRuntime(1335):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-06 14:41:19.659: E/AndroidRuntime(1335):     at android.os.Looper.loop(Looper.java:135)
05-06 14:41:19.659: E/AndroidRuntime(1335):     at android.app.ActivityThread.main(ActivityThread.java:5221)
05-06 14:41:19.659: E/AndroidRuntime(1335):     at java.lang.reflect.Method.invoke(Native Method)
05-06 14:41:19.659: E/AndroidRuntime(1335):     at java.lang.reflect.Method.invoke(Method.java:372)
05-06 14:41:19.659: E/AndroidRuntime(1335):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
05-06 14:41:19.659: E/AndroidRuntime(1335):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
05-06 14:41:19.659: E/AndroidRuntime(1335): Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
05-06 14:41:19.659: E/AndroidRuntime(1335):     at com.example.triptych4.MainActivity.onCreate(MainActivity.java:37)
05-06 14:41:19.659: E/AndroidRuntime(1335):     at android.app.Activity.performCreate(Activity.java:5933)
05-06 14:41:19.659: E/AndroidRuntime(1335):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
05-06 14:41:19.659: E/AndroidRuntime(1335):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
05-06 14:41:19.659: E/AndroidRuntime(1335):     ... 10 more
05-06 14:41:55.802: I/Process(1335): Sending signal. PID: 1335 SIG: 9
05-06 14:42:21.608: I/art(1378): Background partial concurrent mark sweep GC freed 117(17KB) AllocSpace objects, 0(0B) LOS objects, 25% free, 2MB/2MB, paused 11.483ms total 365.174ms
05-06 14:42:22.181: D/AndroidRuntime(1378): Shutting down VM
05-06 14:42:22.273: E/AndroidRuntime(1378): FATAL EXCEPTION: main
05-06 14:42:22.273: E/AndroidRuntime(1378): Process: com.example.triptych4, PID: 1378
05-06 14:42:22.273: E/AndroidRuntime(1378): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.triptych4/com.example.triptych4.MainActivity}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
05-06 14:42:22.273: E/AndroidRuntime(1378):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
05-06 14:42:22.273: E/AndroidRuntime(1378):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
05-06 14:42:22.273: E/AndroidRuntime(1378):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
05-06 14:42:22.273: E/AndroidRuntime(1378):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
05-06 14:42:22.273: E/AndroidRuntime(1378):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-06 14:42:22.273: E/AndroidRuntime(1378):     at android.os.Looper.loop(Looper.java:135)
05-06 14:42:22.273: E/AndroidRuntime(1378):     at android.app.ActivityThread.main(ActivityThread.java:5221)
05-06 14:42:22.273: E/AndroidRuntime(1378):     at java.lang.reflect.Method.invoke(Native Method)
05-06 14:42:22.273: E/AndroidRuntime(1378):     at java.lang.reflect.Method.invoke(Method.java:372)
05-06 14:42:22.273: E/AndroidRuntime(1378):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
05-06 14:42:22.273: E/AndroidRuntime(1378):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
05-06 14:42:22.273: E/AndroidRuntime(1378): Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
05-06 14:42:22.273: E/AndroidRuntime(1378):     at com.example.triptych4.MainActivity.onCreate(MainActivity.java:37)
05-06 14:42:22.273: E/AndroidRuntime(1378):     at android.app.Activity.performCreate(Activity.java:5933)
05-06 14:42:22.273: E/AndroidRuntime(1378):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
05-06 14:42:22.273: E/AndroidRuntime(1378):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
05-06 14:42:22.273: E/AndroidRuntime(1378):     ... 10 more
05-06 14:47:24.069: I/Process(1378): Sending signal. PID: 1378 SIG: 9
05-06 14:50:00.607: I/art(1428): Background sticky concurrent mark sweep GC freed 1722(89KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 2MB/2MB, paused 1.353ms total 189.333ms
05-06 14:50:00.742: D/AndroidRuntime(1428): Shutting down VM
05-06 14:50:00.755: E/AndroidRuntime(1428): FATAL EXCEPTION: main
05-06 14:50:00.755: E/AndroidRuntime(1428): Process: com.example.triptych4, PID: 1428
05-06 14:50:00.755: E/AndroidRuntime(1428): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.triptych4/com.example.triptych4.MainActivity}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
05-06 14:50:00.755: E/AndroidRuntime(1428):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
05-06 14:50:00.755: E/AndroidRuntime(1428):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
05-06 14:50:00.755: E/AndroidRuntime(1428):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
05-06 14:50:00.755: E/AndroidRuntime(1428):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
05-06 14:50:00.755: E/AndroidRuntime(1428):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-06 14:50:00.755: E/AndroidRuntime(1428):     at android.os.Looper.loop(Looper.java:135)
05-06 14:50:00.755: E/AndroidRuntime(1428):     at android.app.ActivityThread.main(ActivityThread.java:5221)
05-06 14:50:00.755: E/AndroidRuntime(1428):     at java.lang.reflect.Method.invoke(Native Method)
05-06 14:50:00.755: E/AndroidRuntime(1428):     at java.lang.reflect.Method.invoke(Method.java:372)
05-06 14:50:00.755: E/AndroidRuntime(1428):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
05-06 14:50:00.755: E/AndroidRuntime(1428):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
05-06 14:50:00.755: E/AndroidRuntime(1428): Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
05-06 14:50:00.755: E/AndroidRuntime(1428):     at com.example.triptych4.MainActivity.onCreate(MainActivity.java:37)
05-06 14:50:00.755: E/AndroidRuntime(1428):     at android.app.Activity.performCreate(Activity.java:5933)
05-06 14:50:00.755: E/AndroidRuntime(1428):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
05-06 14:50:00.755: E/AndroidRuntime(1428):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
05-06 14:50:00.755: E/AndroidRuntime(1428):     ... 10 more
05-06 14:50:08.090: I/Process(1428): Sending signal. PID: 1428 SIG: 9
05-06 14:50:31.291: I/art(1467): Background partial concurrent mark sweep GC freed 79(16KB) AllocSpace objects, 0(0B) LOS objects, 24% free, 2MB/2MB, paused 61.291ms total 133.616ms
05-06 14:50:31.352: D/AndroidRuntime(1467): Shutting down VM
05-06 14:50:31.363: E/AndroidRuntime(1467): FATAL EXCEPTION: main
05-06 14:50:31.363: E/AndroidRuntime(1467): Process: com.example.triptych4, PID: 1467
05-06 14:50:31.363: E/AndroidRuntime(1467): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.triptych4/com.example.triptych4.MainActivity}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
05-06 14:50:31.363: E/AndroidRuntime(1467):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
05-06 14:50:31.363: E/AndroidRuntime(1467):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
05-06 14:50:31.363: E/AndroidRuntime(1467):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
05-06 14:50:31.363: E/AndroidRuntime(1467):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
05-06 14:50:31.363: E/AndroidRuntime(1467):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-06 14:50:31.363: E/AndroidRuntime(1467):     at android.os.Looper.loop(Looper.java:135)
05-06 14:50:31.363: E/AndroidRuntime(1467):     at android.app.ActivityThread.main(ActivityThread.java:5221)
05-06 14:50:31.363: E/AndroidRuntime(1467):     at java.lang.reflect.Method.invoke(Native Method)
05-06 14:50:31.363: E/AndroidRuntime(1467):     at java.lang.reflect.Method.invoke(Method.java:372)
05-06 14:50:31.363: E/AndroidRuntime(1467):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
05-06 14:50:31.363: E/AndroidRuntime(1467):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
05-06 14:50:31.363: E/AndroidRuntime(1467): Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
05-06 14:50:31.363: E/AndroidRuntime(1467):     at com.example.triptych4.MainActivity.onCreate(MainActivity.java:37)
05-06 14:50:31.363: E/AndroidRuntime(1467):     at android.app.Activity.performCreate(Activity.java:5933)
05-06 14:50:31.363: E/AndroidRuntime(1467):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
05-06 14:50:31.363: E/AndroidRuntime(1467):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
05-06 14:50:31.363: E/AndroidRuntime(1467):     ... 10 more

3 个答案:

答案 0 :(得分:0)

在您的Xml中,您有ImageButton

 <ImageButton
    android:id="@+id/button_camera"
    .... />

但是在您的MainActivity中,您将其用作Button

Button cameraButton = (Button)
            findViewById(R.id.button_camera);

将其更改为ImageButton

ImageButton cameraButton = (ImageButton)
            findViewById(R.id.button_camera);

答案 1 :(得分:0)

您尝试将ImageButton强制转换为Button,因此请替换此代码:

ImageButton cameraButton = (ImageButton) findViewById(R.id.button_camera); 

答案 2 :(得分:0)

您正在将ImageButton cameraButton投射到Button

使用

ImageButton cameraButton = (ImageButton)
                findViewById(R.id.button_camera);

而不是

Button cameraButton = (Button)
                findViewById(R.id.button_camera);