Unfortunately, (app name) has stopped. [android in eclipse with AVD]

405 views Asked by At

I have been trying to build a program to capture photo(s) using this code:

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 );
    }
}

When I run the program using AVD in eclipse juno, it shows me: "Unfortunately, (app) has stopped"

I didn't find any error in the program. How can I fix this?

This is my log cat output:

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

And this is my 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>

This is my Android manifest:

<?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>
3

There are 3 answers

1
Lamorak On

Read the error logs carefully!

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

The root RelativeLayout in your XML does not have layout_width nor layout_height parameters.

0
nomanr On

Replace your main activity xml file with this.

<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>

You haven't added layout_height and layout_width attributes in your parent RelativeLayout.

2
Shoeb Siddique On

You missed to give the height and width of your Parent Layout

android:layout_width="match_parent" android:layout_height="match_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>