Java 不幸的是,(应用程序名称)已停止。[带AVD的eclipse中的android]

Java 不幸的是,(应用程序名称)已停止。[带AVD的eclipse中的android],java,android,android-layout,avd,Java,Android,Android Layout,Avd,我一直在尝试构建一个程序,使用以下代码捕获照片: package com.example.capturephoto; import android.support.v7.app.ActionBarActivity; import java.io.File; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.Bitm

我一直在尝试构建一个程序,使用以下代码捕获照片:

package com.example.capturephoto;

import android.support.v7.app.ActionBarActivity;
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

    protected Button _button;
    protected ImageView _image;
    protected TextView _field;
    protected String _path;
    protected boolean _taken;

    protected static final String PHOTO_TAKEN   = "photo_taken";

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        _image = ( ImageView ) findViewById( R.id.image );
        _field = ( TextView ) findViewById( R.id.field );
        _button = ( Button ) findViewById( R.id.button );
        _button.setOnClickListener( new ButtonClickHandler() );

        _path = Environment.getExternalStorageDirectory() + "/images/make_machine_example.jpg";
    }

    public class ButtonClickHandler implements View.OnClickListener 
    {
        public void onClick( View view ){
            Log.i("MakeMachine", "ButtonClickHandler.onClick()" );
            startCameraActivity();
        }
    }

    protected void startCameraActivity()
    {
        Log.i("MakeMachine", "startCameraActivity()" );
        File file = new File( _path );
        Uri outputFileUri = Uri.fromFile( file );

        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
        intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );

        startActivityForResult( intent, 0 );
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {   
        Log.i( "MakeMachine", "resultCode: " + resultCode );
        switch( resultCode )
        {
            case 0:
                Log.i( "MakeMachine", "User cancelled" );
                break;

            case -1:
                onPhotoTaken();
                break;
        }
    }

    protected void onPhotoTaken()
    {
        Log.i( "MakeMachine", "onPhotoTaken" );

        _taken = true;

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 4;

        Bitmap bitmap = BitmapFactory.decodeFile( _path, options );

        _image.setImageBitmap(bitmap);
        _field.setVisibility( View.GONE );
    }

    @Override 
    protected void onRestoreInstanceState( Bundle savedInstanceState){
        Log.i( "MakeMachine", "onRestoreInstanceState()");
        if( savedInstanceState.getBoolean( MainActivity.PHOTO_TAKEN ) ) {
            onPhotoTaken();
        }
    }

    @Override
    protected void onSaveInstanceState( Bundle outState ) {
        outState.putBoolean( MainActivity.PHOTO_TAKEN, _taken );
    }
}
当我在EclipseJuno中使用AVD运行程序时,它显示:不幸的是,应用程序已经停止

我在程序中没有发现任何错误。我怎样才能解决这个问题

这是我的日志cat输出:

06-16 03:44:05.630: D/AndroidRuntime(1086): Shutting down VM
06-16 03:44:05.630: W/dalvikvm(1086): threadid=1: thread exiting with uncaught exception (group=0xb3a7bba8)
06-16 03:44:05.650: E/AndroidRuntime(1086): FATAL EXCEPTION: main
06-16 03:44:05.650: E/AndroidRuntime(1086): Process: com.example.capturephoto, PID: 1086
06-16 03:44:05.650: E/AndroidRuntime(1086): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.capturephoto/com.example.capturephoto.MainActivity}: java.lang.RuntimeException: Binary XML file line #1: You must supply a layout_width attribute.
06-16 03:44:05.650: E/AndroidRuntime(1086):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-16 03:44:05.650: E/AndroidRuntime(1086):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-16 03:44:05.650: E/AndroidRuntime(1086):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-16 03:44:05.650: E/AndroidRuntime(1086):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-16 03:44:05.650: E/AndroidRuntime(1086):     at android.os.Handler.dispatchMessage(Handler.java:102)
06-16 03:44:05.650: E/AndroidRuntime(1086):     at android.os.Looper.loop(Looper.java:136)
06-16 03:44:05.650: E/AndroidRuntime(1086):     at android.app.ActivityThread.main(ActivityThread.java:5017)
06-16 03:44:05.650: E/AndroidRuntime(1086):     at java.lang.reflect.Method.invokeNative(Native Method)
06-16 03:44:05.650: E/AndroidRuntime(1086):     at java.lang.reflect.Method.invoke(Method.java:515)
06-16 03:44:05.650: E/AndroidRuntime(1086):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-16 03:44:05.650: E/AndroidRuntime(1086):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-16 03:44:05.650: E/AndroidRuntime(1086):     at dalvik.system.NativeStart.main(Native Method)
06-16 03:44:05.650: E/AndroidRuntime(1086): Caused by: java.lang.RuntimeException: Binary XML file line #1: You must supply a layout_width attribute.
06-16 03:44:05.650: E/AndroidRuntime(1086):     at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:492)
06-16 03:44:05.650: E/AndroidRuntime(1086):     at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:5948)
06-16 03:44:05.650: E/AndroidRuntime(1086):     at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:6117)
06-16 03:44:05.650: E/AndroidRuntime(1086):     at android.widget.FrameLayout$LayoutParams.<init>(FrameLayout.java:615)
06-16 03:44:05.650: E/AndroidRuntime(1086):     at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:559)
06-16 03:44:05.650: E/AndroidRuntime(1086):     at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:56)
06-16 03:44:05.650: E/AndroidRuntime(1086):     at android.view.LayoutInflater.inflate(LayoutInflater.java:480)
06-16 03:44:05.650: E/AndroidRuntime(1086):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
06-16 03:44:05.650: E/AndroidRuntime(1086):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
06-16 03:44:05.650: E/AndroidRuntime(1086):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
06-16 03:44:05.650: E/AndroidRuntime(1086):     at android.app.Activity.setContentView(Activity.java:1929)
06-16 03:44:05.650: E/AndroidRuntime(1086):     at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:217)
06-16 03:44:05.650: E/AndroidRuntime(1086):     at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:110)
06-16 03:44:05.650: E/AndroidRuntime(1086):     at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:77)
06-16 03:44:05.650: E/AndroidRuntime(1086):     at com.example.capturephoto.MainActivity.onCreate(MainActivity.java:35)
06-16 03:44:05.650: E/AndroidRuntime(1086):     at android.app.Activity.performCreate(Activity.java:5231)
06-16 03:44:05.650: E/AndroidRuntime(1086):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-16 03:44:05.650: E/AndroidRuntime(1086):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-16 03:44:05.650: E/AndroidRuntime(1086):     ... 11 more
06-16 03:44:15.280: I/Process(1086): Sending signal. PID: 1086 SIG: 9
这是我的XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.capturephoto.MainActivity" >

    <RelativeLayout android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="6"
                    android:gravity="center_horizontal|center_vertical">

        <TextView android:id="@+id/field"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:text="No Image"
                  android:gravity="center_horizontal"/>

        <ImageView android:id="@+id/image"
                   android:layout_width="fill_parent"
                   android:layout_height="wrap_content"/>

    </RelativeLayout>

    <Button android:id="@+id/button"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Take Photo"
            android:layout_weight="1"/>

</RelativeLayout>
这是我的Android清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.capturephoto"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="5"
        android:targetSdkVersion="14" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat" >
        <activity
            android:name="com.example.capturephoto.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

仔细阅读错误日志

Binary XML file line #1: You must supply a layout_width attribute.

XML中的根RelativeLayout没有布局宽度或布局高度参数。

用此参数替换主活动XML文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.capturephoto.MainActivity" >

<RelativeLayout android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="6"
                android:gravity="center_horizontal|center_vertical">

    <TextView android:id="@+id/field"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="No Image"
              android:gravity="center_horizontal"/>

    <ImageView android:id="@+id/image"
               android:layout_width="fill_parent"
               android:layout_height="wrap_content"/>

</RelativeLayout>

<Button android:id="@+id/button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Take Photo"
        android:layout_weight="1"/>

</RelativeLayout>

您尚未在父级RelativeLayout中添加布局高度和布局宽度属性。

您没有提供父级布局的高度和宽度

android:layout\u width=match\u parent android:layout\u height=match\u parent

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.capturephoto.MainActivity" >


</RelativeLayout>

定义父关系的布局宽度和布局高度我是安卓系统中的乞丐,所以我真的看不到日志中的一些错误。但是谢谢你回答我的问题,这给了我更多的经验。^^工作:D非常感谢max ^^^是的,我错过了。谢谢shoeb,它非常有用^^^@MaysarohSyafa'atin请将其标记为正确。如有任何疑问,请随时与我联系::