简体   繁体   中英

Android: Unfortunately <app name> has stopped

I'm new to android development, this is just my second app i built just to kill time, though, i could'nt understand, what caused this error. Need expert advice

The activity_starting_point.xml:

<LinearLayout 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:orientation="vertical"
    tools:context=".StatingPoint" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="35dp"
        android:text="Enter Number 1"
          android:id="@+id/tvDisplay"
           />

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

        <requestFocus />
    </EditText>

    <TextView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="35dp"
        android:text="Enter Number 2" 
          android:id="@+id/tvDisplay"
        />

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

    <Button
        android:id="@+id/button1"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:text="Add"
        android:layout_gravity="center" />

    <Button
        android:id="@+id/button1"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:text="Subtract"
        android:layout_gravity="center" />

       <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" Your Total is 0 "  
        android:textSize="45dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:id="@+id/tvDisplay" />

</LinearLayout>

The Java class file: StartingPoint.java package com.ankur.calulator;

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


public class StatingPoint extends Activity {


    int num1,num2;
    int total;
    Button add,sub;
    TextView display;
    EditText no1,no2;



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

        total=0;
        no1=(EditText) findViewById(R.id.editText1);
        no2=(EditText) findViewById(R.id.editText2);
        display=(TextView)findViewById(R.id.tvDisplay);

        add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                total=num1+num2;
                display.setText("Your Total is "+total);

            }
        });

        sub.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                total=num1-num2;
                display.setText("Your Total is "+total);

            }
        });
    }

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

}

The logcat file: its got blank, i think i messed up with it, though i saw something as java null pointer exception before i knw i'm close, just nt able to find some missing link, need advice.

You haven't initialized add and sub like you do for no1 , no1 and display

And you are trying to call setOnClickListener on add and sub without initializing so you get a NullPointerException

You need to provide different id to add and sub and initialize it.

In your activity_starting_point.xml , you have:

  • 3 TextViews with the same id: tvDisplay
  • 2 Buttons with the same id: button1

Please assign unique AND meaningful IDs to be able to reference them later in your java code, eg:

android:id="@+id/btAdd"

and

android:id="@+id/btSubtract"

(NOTE: you actually don't need id for TextViews with instructions, eg. all those tvDisplay, unless you want to actually reference them in Java, for example to change the instruction text)

Next, as spotted by Apoorv, whereas you initialize your no1 and no2, you don't do it with add and sub, hence they are null. Just initialise them the same way the same way as the above:

add = (Button) findViewById(R.id.btAdd);

and

sub = (Button) findViewById(R.id.btSubtract);

Also, in your OnClickListeners, your calculations will always yield 0 for total, because nowhere in the code you're actually getting values for num1 and num2. You probably want to do something like:

num1 = Integer.parseInt(no1.getText().toString());
num2 = Integer.parseInt(no2.getText().toString());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM