Android错误:应用程序;“应用程序名称”;意外地停止了。请再试一次

Android错误:应用程序;“应用程序名称”;意外地停止了。请再试一次,android,android-emulator,Android,Android Emulator,我是Android新手,在尝试启动打开相机、拍摄照片然后裁剪结果的应用程序时出现了这个错误 我会附上一些代码,也许你能帮我 谢谢 $java代码 public class MainActivity extends Activity implements OnClickListener { //keep track of camera capture intent final int CAMERA_CAPTURE = 1; //keep track of cropping int

我是Android新手,在尝试启动打开相机、拍摄照片然后裁剪结果的应用程序时出现了这个错误

我会附上一些代码,也许你能帮我

谢谢

$java代码

public class MainActivity extends Activity implements OnClickListener {

//keep track of camera capture intent
    final int CAMERA_CAPTURE = 1;
    //keep track of cropping intent
    final int PIC_CROP = 2;
    //captured picture uri
    private Uri picUri;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //retrieve a reference to the UI button
        Button captureBtn = (Button)findViewById(R.id.picture);
        //handle button clicks
        captureBtn.setOnClickListener(this);
    }

    /**
     * Click method to handle user pressing button to launch camera
     */
    public void onClick(View v) {
        if (v.getId() == R.id.picture) {     
            try {
                //use standard intent to capture an image
                Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                //we will handle the returned data in onActivityResult
                startActivityForResult(captureIntent, CAMERA_CAPTURE);
            }
            catch(ActivityNotFoundException anfe){
                //display an error message
                String errorMessage = "Whoops - your device doesn't support capturing images!";
                Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
                toast.show();
            }
        }
    }

    /**
     * Handle user returning from both capturing and cropping the image
     */
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            //user is returning from capturing an image using the camera
            if(requestCode == CAMERA_CAPTURE){
                //get the Uri for the captured image
                picUri = data.getData();
                //carry out the crop operation
                performCrop();
            }
            //user is returning from cropping the image
            else if(requestCode == PIC_CROP){
                //get the returned data
                Bundle extras = data.getExtras();
                //get the cropped bitmap
                Bitmap thePic = extras.getParcelable("data");
                //retrieve a reference to the ImageView
                ImageView picView = (ImageView)findViewById(R.id.picture);
                //display the returned cropped image
                picView.setImageBitmap(thePic);
            }
        }
    }

    /**
     * Helper method to carry out crop operation
     */
    private void performCrop(){
        //take care of exceptions
        try {
            //call the standard crop action intent (the user device may not support it)
            Intent cropIntent = new Intent("com.android.camera.action.CROP"); 
            //indicate image type and Uri
            cropIntent.setDataAndType(picUri, "image/*");
            //set crop properties
            cropIntent.putExtra("crop", "true");
            //indicate aspect of desired crop
            cropIntent.putExtra("aspectX", 1);
            cropIntent.putExtra("aspectY", 1);
            //indicate output X and Y
            cropIntent.putExtra("outputX", 256);
            cropIntent.putExtra("outputY", 256);
            //retrieve data on return
            cropIntent.putExtra("return-data", true);
            //start the activity - we handle returning in onActivityResult
            startActivityForResult(cropIntent, PIC_CROP);  
        }
        //respond to users whose devices do not support the crop action
        catch(ActivityNotFoundException anfe){
            //display an error message
            String errorMessage = "Whoops - your device doesn't support the crop action!";
            Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
            toast.show();
        }
    }
}
$xml主要活动

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/intro"
    android:layout_margin="3dp"
    android:textStyle="bold" />
<Button
android:id="@+id/capture_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/capture"
/>
<ImageView 
    android:id="@+id/picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/picture"
android:layout_margin="5dp"
android:background="@drawable/pic_border" />

</LinearLayout>
改变


“picture”是ImageView的id,您不能将其强制转换为按钮

Post您的logcat traceI编辑了文章。谢谢你,很抱歉回复晚了。。其他人已经提供了答案…好的。。。无论如何谢谢你。。。它可以访问程序,但不会打开网络摄像头。你知道什么原因吗?你需要在Android中添加权限谢谢…它现在可以工作了。。。但它仍然无法打开相机。。。你知道什么原因吗?
<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="intro">Capture a picture to crop!</string>
<string name="app_name">Shoot And Crop</string>
<string name="picture">Picture</string>
<string name="capture">Launch Camera</string>

</resources>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true">
<gradient
     android:startColor="#99ffffff"
     android:endColor="#99ffffff"
     android:centerColor="#00000000"
     android:angle="90" />

<padding android:left="10dp" android:top="10dp"
        android:right="10dp" android:bottom="10dp" />
<corners android:radius="5dp" />
<stroke
    android:width="2dp"
    android:color="#ccffffff"
    />
</shape>
04-14 14:48:04.590: D/AndroidRuntime(894): Shutting down VM
04-14 14:48:04.602: W/dalvikvm(894): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-14 14:48:04.720: E/AndroidRuntime(894): FATAL EXCEPTION: main
04-14 14:48:04.720: E/AndroidRuntime(894): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.camera/com.example.camera.MainActivity}: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.Button
04-14 14:48:04.720: E/AndroidRuntime(894):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-14 14:48:04.720: E/AndroidRuntime(894):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-14 14:48:04.720: E/AndroidRuntime(894):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-14 14:48:04.720: E/AndroidRuntime(894):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-14 14:48:04.720: E/AndroidRuntime(894):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-14 14:48:04.720: E/AndroidRuntime(894):  at android.os.Looper.loop(Looper.java:137)
04-14 14:48:04.720: E/AndroidRuntime(894):  at android.app.ActivityThread.main(ActivityThread.java:5041)
04-14 14:48:04.720: E/AndroidRuntime(894):  at java.lang.reflect.Method.invokeNative(Native Method)
04-14 14:48:04.720: E/AndroidRuntime(894):  at java.lang.reflect.Method.invoke(Method.java:511)
04-14 14:48:04.720: E/AndroidRuntime(894):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-14 14:48:04.720: E/AndroidRuntime(894):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-14 14:48:04.720: E/AndroidRuntime(894):  at dalvik.system.NativeStart.main(Native Method)
04-14 14:48:04.720: E/AndroidRuntime(894): Caused by: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.Button
04-14 14:48:04.720: E/AndroidRuntime(894):  at com.example.camera.MainActivity.onCreate(MainActivity.java:32)
04-14 14:48:04.720: E/AndroidRuntime(894):  at android.app.Activity.performCreate(Activity.java:5104)
04-14 14:48:04.720: E/AndroidRuntime(894):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-14 14:48:04.720: E/AndroidRuntime(894):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-14 14:48:04.720: E/AndroidRuntime(894):  ... 11 more
04-14 14:53:50.511: D/AndroidRuntime(1027): Shutting down VM
04-14 14:53:50.511: W/dalvikvm(1027): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-14 14:53:50.531: E/AndroidRuntime(1027): FATAL EXCEPTION: main
04-14 14:53:50.531: E/AndroidRuntime(1027): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.camera/com.example.camera.MainActivity}: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.Button
04-14 14:53:50.531: E/AndroidRuntime(1027):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-14 14:53:50.531: E/AndroidRuntime(1027):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-14 14:53:50.531: E/AndroidRuntime(1027):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-14 14:53:50.531: E/AndroidRuntime(1027):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-14 14:53:50.531: E/AndroidRuntime(1027):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-14 14:53:50.531: E/AndroidRuntime(1027):     at android.os.Looper.loop(Looper.java:137)
04-14 14:53:50.531: E/AndroidRuntime(1027):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-14 14:53:50.531: E/AndroidRuntime(1027):     at java.lang.reflect.Method.invokeNative(Native Method)
04-14 14:53:50.531: E/AndroidRuntime(1027):     at java.lang.reflect.Method.invoke(Method.java:511)
04-14 14:53:50.531: E/AndroidRuntime(1027):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-14 14:53:50.531: E/AndroidRuntime(1027):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-14 14:53:50.531: E/AndroidRuntime(1027):     at dalvik.system.NativeStart.main(Native Method)
04-14 14:53:50.531: E/AndroidRuntime(1027): Caused by: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.Button
04-14 14:53:50.531: E/AndroidRuntime(1027):     at com.example.camera.MainActivity.onCreate(MainActivity.java:32)
04-14 14:53:50.531: E/AndroidRuntime(1027):     at android.app.Activity.performCreate(Activity.java:5104)
04-14 14:53:50.531: E/AndroidRuntime(1027):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-14 14:53:50.531: E/AndroidRuntime(1027):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-14 14:53:50.531: E/AndroidRuntime(1027):     ... 11 more
04-14 14:53:53.302: I/Process(1027): Sending signal. PID: 1027 SIG: 9
Button captureBtn = (Button)findViewById(R.id.picture);
Button captureBtn = (Button)findViewById(R.id.capture_btn);