Tech Nerd
Tech Nerd

Reputation: 832

Unfortunately HelloWorld Stopped (My app stopped on emulator: Unfortunately )

I am completely a beginner android developer but a good Java J2SE Developer. I just want to know that while I am working on first Android app: after compiling it and having no errors facing problems running it in an emulator! my code for java class is: package com.example.helloworld;

 import android.os.Bundle;
 import android.app.Activity;
 import android.view.Menu;
 import android.widget.Button;

public class MainActivity extends Activity {

//Variables Declaration
private Button one;
private Button two;
private Button three;
private Button four;
private Button five;
private Button six;
private Button seven;
private Button eight;
private Button nine;
private Button plus;
private Button minus;
private Button divide;
private Button multiply;
private Button zero;
private Button dot;
private Button equal;
private Button clr;
//End of Variables Declaration

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

    setContentView(R.layout.activity_main);
    addListenerOnButton();



}

public void addListenerOnButton(){
    one=(Button) this.findViewById(R.id.button1);
    two=(Button) this.findViewById(R.id.button2);
    three=(Button) this.findViewById(R.id.button3);
    four=(Button) this.findViewById(R.id.button4);
    five=(Button) this.findViewById(R.id.button5);
    six=(Button) this.findViewById(R.id.button6);
    seven=(Button) this.findViewById(R.id.button7);
    eight=(Button) this.findViewById(R.id.button8);
    nine=(Button) this.findViewById(R.id.button9);
    zero=(Button) this. findViewById(R.id.button14);
    dot=(Button) this.findViewById(R.id.button15);
    plus=(Button) this.findViewById(R.id.button13);
    equal=(Button) this.findViewById(R.id.button16);
    clr=(Button) this.findViewById(R.id.button17);
}


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


 }

the .xml is:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@style/AppBaseTheme"
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=".MainActivity" >

<LinearLayout
    android:id="@+id/lay"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/numf2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="numberDecimal" />

    <EditText
        android:id="@+id/opf"
        android:layout_width="290dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:inputType="numberDecimal|text" />

    <EditText
        android:id="@+id/numf1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="26dp"
        android:ems="10"
        android:inputType="numberDecimal" >

        <requestFocus />
    </EditText>
</LinearLayout>

<Button
    android:id="@+id/button2"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/button1"
    android:layout_toRightOf="@+id/button1"
    android:text="2"
     />

<Button
    android:id="@+id/button3"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/button2"
    android:layout_alignBottom="@+id/button2"
    android:layout_toRightOf="@+id/button2"
    android:text="3"
     />

<Button
    android:id="@+id/button4"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button1"
    android:layout_alignLeft="@+id/button1"
    android:text="4" 
    />

<Button
    android:id="@+id/button5"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/button4"
    android:layout_alignBottom="@+id/button4"
    android:layout_toLeftOf="@+id/button3"
    android:text="5"
     />

<Button
    android:id="@+id/button6"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/button5"
    android:layout_alignBottom="@+id/button5"
    android:layout_alignLeft="@+id/button3"
    android:text="6"
     />

<Button
    android:id="@+id/button7"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button4"
    android:layout_alignLeft="@+id/button4"
    android:text="7" 
    />

<Button
    android:id="@+id/button8"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button5"
    android:layout_alignLeft="@+id/button5"
    android:text="8"
     />

<Button
    android:id="@+id/button9"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button6"
    android:layout_alignLeft="@+id/button6"
    android:text="9"
     />

<Button
    android:id="@+id/button10"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/button9"
    android:layout_toRightOf="@+id/button6"
    android:text="Button" />

<Button
    android:id="@+id/button11"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/button10"
    android:layout_alignTop="@+id/button6"
    android:text="Button" />

<Button
    android:id="@+id/button12"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button11"
    android:layout_below="@+id/button11"
    android:text="Button" />

<Button
    android:id="@+id/button13"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button12"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/button12"
    android:text="+" />

<Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button13"
    android:layout_alignLeft="@+id/lay"
    android:text="1" />

<Button
    android:id="@+id/button14"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/button13"
    android:layout_alignBottom="@+id/button13"
    android:layout_alignLeft="@+id/button1"
    android:layout_alignRight="@+id/button2"
    android:text="0" />

<Button
    android:id="@+id/button15"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button3"
    android:layout_alignRight="@+id/button3"
    android:layout_alignTop="@+id/button13"
    android:text="dot" />

<Button
    android:id="@+id/button16"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/button13"
    android:layout_alignBottom="@+id/button13"
    android:layout_alignRight="@+id/lay"
    android:layout_toRightOf="@+id/button12"
    android:text="=" />

<Button
    android:id="@+id/button17"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button16"
    android:layout_alignRight="@+id/lay"
    android:layout_alignTop="@+id/button10"
    android:text="CLR" />

 </RelativeLayout>

please help me I have searched a lot but failed to find any solution my app does't start at all and unfortunately it stopped. Thanks in advance

Upvotes: 0

Views: 1370

Answers (1)

Siddharth Lele
Siddharth Lele

Reputation: 27748

I tested your code (the XML as well as the Java) in a quick test project. It crashes all right. Apparently, the layout does not like android:background attribute set in it.

Removing this line from the parent RelativeLayout will fix it:

android:background="@style/AppBaseTheme"

Testing on my device:

enter image description here

The final parent RelativeLayout will look this this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    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=".MainActivity" >

    ....

    ....

</RelativeLayout>

As to why it does not like that attribute is because it maps to a style in the styles.xml called: AppBaseTheme. If you have created the project using the Android Application Project wizard, as I did, I have noticed it creates a blank styles.xml with just this in it:

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>

Naturally, the attribute android:background="@style/AppBaseTheme" does not have the necessary that will supply the RelativeLayout with a resource that is compatible with the android:background attribute.

Upvotes: 1

Related Questions