不幸的是app已停止在Android模拟器上

时间:2015-06-26 20:09:24

标签: android android-edittext

这是我的main.java

package com.example.citylife;

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class MainActivity extends Activity {
Button btn1,btn2,btn3,btn4,btn5,btn6,btn7;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn1=(Button) findViewById(R.id.button1);
    btn2=(Button) findViewById(R.id.button2);
    btn3=(Button) findViewById(R.id.button3);
    btn4=(Button) findViewById(R.id.button4);
    btn5=(Button) findViewById(R.id.button5);
    btn6=(Button) findViewById(R.id.button6);
    btn7=(Button) findViewById(R.id.button7);
    btn1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent obj1=new Intent(MainActivity.this,Hotels.class);
            startActivity(obj1);

        }
    });
    btn2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent obj2=new Intent(MainActivity.this,Cabs.class);
            startActivity(obj2);

        }
    });
    btn3.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent obj3=new Intent(MainActivity.this,Hospitals.class);
            startActivity(obj3);

        }
    });
    btn4.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent obj4=new Intent(MainActivity.this,Monuments.class);
            startActivity(obj4);

        }
    });
    btn5.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent obj5=new Intent(MainActivity.this,Theatres.class);
            startActivity(obj5);

        }
    });
    btn6.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent obj6=new Intent(MainActivity.this,Malls.class);
            startActivity(obj6);

        }
    });
    btn7.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent obj7=new       Intent(MainActivity.this,ExpenseCalculator.class);
            startActivity(obj7);

        }
    });

}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

这是我的main.xml

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnCount="1"
android:rowCount="12" 
android:background="@color/skyblue">

<Button
    android:id="@+id/button1"
    android:layout_width="290dp"
    android:layout_column="0"
    android:layout_gravity="center"
    android:layout_marginTop="30dp"
    android:layout_row="0"
    android:text="HOTELS" 
    android:textStyle="bold|italic"
    android:textColor="@color/darkblue"/>

<Button
    android:id="@+id/button2"
    android:layout_width="290dp"
    android:layout_column="0"
    android:layout_gravity="center"
    android:layout_marginTop="15dp"
    android:layout_row="1"
    android:text="CABS" 
    android:textStyle="bold|italic"
    android:textColor="@color/darkblue"/>

<Button
    android:id="@+id/button3"
    android:layout_width="290dp"
    android:layout_column="0"
    android:layout_gravity="center"
    android:layout_row="2"
    android:text="HOSPITALS" 
    android:layout_marginTop="15dp"
    android:textStyle="bold|italic"
    android:textColor="@color/darkblue"/>

<Button
    android:id="@+id/button4"
    android:layout_width="290dp"
    android:layout_column="0"
    android:layout_gravity="center"
    android:layout_row="3"
    android:text="MONUMENTS" 
    android:layout_marginTop="15dp"
    android:textStyle="bold|italic"
    android:textColor="@color/darkblue"/>

<Button
    android:id="@+id/button5"
    android:layout_width="290dp"
    android:layout_column="0"
    android:layout_gravity="center"
    android:layout_row="4"
    android:text="THEATRES" 
    android:layout_marginTop="15dp"
    android:textStyle="bold|italic"
    android:textColor="@color/darkblue"/>

<Button
    android:id="@+id/button6"
    android:layout_width="290dp"
    android:layout_column="0"
    android:layout_gravity="center"
    android:layout_row="5"
    android:text="MALLS" 
    android:layout_marginTop="15dp"
    android:textStyle="bold|italic"
    android:textColor="@color/darkblue"/>

<Button
    android:id="@+id/button7"
    android:layout_width="290dp"
    android:layout_column="0"
    android:layout_gravity="center"
    android:layout_row="6"
    android:text="EXPENSE CALCULATOR" 
    android:layout_marginTop="15dp"
    android:textStyle="bold|italic"
    android:textColor="@color/darkblue"/>

当我点击费用计算器时,不幸的是app已停止显示其主要和xml文件在下面给出

package com.example.citylife;

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

public class ExpenseCalculator extends Activity {
EditText ed1,ed2,ed3,ed4,ed5,ed6,ed7;
Button btn1,btn2;
TextView tt1,tt2;
int a1=Integer.parseInt(ed1.getText().toString());
int b2=Integer.parseInt(ed2.getText().toString());
int c3=Integer.parseInt(ed3.getText().toString());
int d4=Integer.parseInt(ed4.getText().toString());
int e5=Integer.parseInt(ed5.getText().toString());
int f6=Integer.parseInt(ed6.getText().toString());
int g7=Integer.parseInt(ed7.getText().toString());
int total = a1 + b2 + c3 + d4 + e5 + f6;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_expense_calculator);
    ed1=(EditText) findViewById(R.id.editText1);
    ed2=(EditText) findViewById(R.id.editText2);
    ed3=(EditText) findViewById(R.id.editText3);
    ed4=(EditText) findViewById(R.id.editText4);
    ed5=(EditText) findViewById(R.id.editText5);
    ed6=(EditText) findViewById(R.id.editText6);
    ed7=(EditText) findViewById(R.id.editText7);
    btn1=(Button) findViewById(R.id.button1);
    btn2=(Button) findViewById(R.id.button2);
    tt1=(TextView) findViewById(R.id.textView7);
    tt2=(TextView) findViewById(R.id.TextView01);

    btn1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            tt1.setText(Integer.toString(total));

        }
    });
    btn2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
        if(g7>total)
            tt2.setText("Go for it !!");
        else
            tt2.setText("Out of budget :(");

        }
    });
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

XML:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnCount="2"
android:rowCount="26" >

<EditText
    android:id="@+id/editText1"
    android:layout_column="1"
    android:layout_gravity="right|top"
    android:layout_marginTop="20dp"
    android:layout_row="1"
    android:ems="10" />

<EditText
    android:id="@+id/editText2"
    android:layout_column="1"
    android:layout_gravity="right|top"
    android:layout_marginTop="20dp"
    android:layout_row="3"
    android:ems="10" />

<EditText
    android:id="@+id/editText3"
    android:layout_column="1"
    android:layout_gravity="right|top"
    android:layout_marginTop="20dp"
    android:layout_row="5"
    android:ems="10" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/editText4"
    android:layout_column="1"
    android:layout_gravity="right|top"
    android:layout_marginTop="20dp"
    android:layout_row="7"
    android:ems="10" />

<EditText
    android:id="@+id/editText5"
    android:layout_column="1"
    android:layout_gravity="right|top"
    android:layout_marginTop="20dp"
    android:layout_row="9"
    android:ems="10" />

<EditText
    android:id="@+id/editText6"
    android:layout_column="1"
    android:layout_gravity="right|top"
    android:layout_marginTop="20dp"
    android:layout_row="10"
    android:ems="10" />

<TextView
    android:id="@+id/textView2"
    android:layout_column="1"
    android:layout_gravity="left|center_vertical"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="20dp"
    android:layout_row="3"
    android:text="CABS:" />

<TextView
    android:id="@+id/textView3"
    android:layout_column="1"
    android:layout_gravity="left|center_vertical"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="20dp"
    android:layout_row="5"
    android:text="HOSPITALS:" />

<TextView
    android:id="@+id/textView4"
    android:layout_column="1"
    android:layout_gravity="left|center_vertical"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="20dp"
    android:layout_row="7"
    android:text="MONUMENTS:" />

<TextView
    android:id="@+id/textView5"
    android:layout_column="1"
    android:layout_gravity="left|center_vertical"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="20dp"
    android:layout_row="9"
    android:text="THEATRES:" />

<TextView
    android:id="@+id/textView6"
    android:layout_column="1"
    android:layout_gravity="left|center_vertical"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="20dp"
    android:layout_row="10"
    android:text="MALLS:" />

<TextView
    android:id="@+id/textView1"
    android:layout_height="wrap_content"
    android:layout_column="1"
    android:layout_gravity="left|center_vertical"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="20dp"
    android:layout_row="1"
    android:text="HOTELS:" />

<Button
    android:id="@+id/button1"
    android:layout_column="1"
    android:layout_gravity="left|top"
    android:layout_marginLeft="15dp"
    android:layout_row="20"
    android:text="Total Expenses" />

<EditText
    android:id="@+id/editText7"
    android:layout_width="160dp"
    android:layout_column="1"
    android:layout_gravity="right|top"
    android:layout_row="21"
    android:ems="10" />

<TextView
    android:id="@+id/textView8"
    android:layout_column="1"
    android:layout_gravity="left|top"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="5dp"
    android:layout_row="21"
    android:text="Your Budget"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
    android:id="@+id/button2"
    style="?android:attr/buttonStyleSmall"
    android:layout_column="1"
    android:layout_gravity="left|top"
    android:layout_row="24"
    android:layout_marginLeft="15dp"
    android:text="Check" />

<TextView
    android:id="@+id/textView7"
    android:layout_width="90dp"
    android:layout_height="wrap_content"
    android:layout_column="1"
    android:layout_gravity="right|center_vertical"
    android:layout_marginRight="50dp"
    android:layout_row="20"
    android:text="TextView"
    android:textSize="21dp" />

<TextView
    android:id="@+id/TextView01"
    android:layout_width="90dp"
    android:layout_height="wrap_content"
    android:layout_column="1"
    android:layout_gravity="right|center_vertical"
    android:layout_marginRight="70dp"
    android:layout_row="24"
    android:text="TextView"
    android:textSize="21dp" 
    android:layout_marginTop="5dp"/>

</GridLayout>

Log cat是:

06-26 15:47:49.540: D/gralloc_goldfish(1001): Emulator without GPU emulation detected.
06-26 15:47:54.660: D/AndroidRuntime(1001): Shutting down VM
06-26 15:47:54.660: W/dalvikvm(1001): threadid=1: thread exiting with uncaught exception (group=0xb2a1dba8)
06-26 15:47:54.850: E/AndroidRuntime(1001): FATAL EXCEPTION: main
06-26 15:47:54.850: E/AndroidRuntime(1001): Process: com.example.citylife, PID: 1001
06-26 15:47:54.850: E/AndroidRuntime(1001): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.citylife/com.example.citylife.ExpenseCalculator}: java.lang.NullPointerException
06-26 15:47:54.850: E/AndroidRuntime(1001):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
06-26 15:47:54.850: E/AndroidRuntime(1001):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-26 15:47:54.850: E/AndroidRuntime(1001):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-26 15:47:54.850: E/AndroidRuntime(1001):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-26 15:47:54.850: E/AndroidRuntime(1001):     at android.os.Handler.dispatchMessage(Handler.java:102)
06-26 15:47:54.850: E/AndroidRuntime(1001):     at android.os.Looper.loop(Looper.java:136)
06-26 15:47:54.850: E/AndroidRuntime(1001):     at android.app.ActivityThread.main(ActivityThread.java:5017)
06-26 15:47:54.850: E/AndroidRuntime(1001):     at java.lang.reflect.Method.invokeNative(Native Method)
06-26 15:47:54.850: E/AndroidRuntime(1001):     at java.lang.reflect.Method.invoke(Method.java:515)
06-26 15:47:54.850: E/AndroidRuntime(1001):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-26 15:47:54.850: E/AndroidRuntime(1001):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-26 15:47:54.850: E/AndroidRuntime(1001):     at dalvik.system.NativeStart.main(Native Method)
06-26 15:47:54.850: E/AndroidRuntime(1001): Caused by: java.lang.NullPointerException
06-26 15:47:54.850: E/AndroidRuntime(1001):     at com.example.citylife.ExpenseCalculator.<init>(ExpenseCalculator.java:17)
06-26 15:47:54.850: E/AndroidRuntime(1001):     at java.lang.Class.newInstanceImpl(Native Method)
06-26 15:47:54.850: E/AndroidRuntime(1001):     at java.lang.Class.newInstance(Class.java:1208)
06-26 15:47:54.850: E/AndroidRuntime(1001):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
06-26 15:47:54.850: E/AndroidRuntime(1001):     at  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
06-26 15:47:54.850: E/AndroidRuntime(1001):     ... 11 more
06-26 15:48:04.230: I/Process(1001): Sending signal. PID: 1001 SIG: 9

我是Android应用开发的新手,无法理解为什么会出现此错误 请帮忙!!!

以下是第一个答案给出更正后的新logcat:

06-27 12:41:17.694: D/gralloc_goldfish(959): Emulator without GPU emulation detected.
06-27 12:41:37.944: D/dalvikvm(959): GC_CONCURRENT freed 173K, 11% free 2625K/2928K, paused 8ms+27ms, total 184ms
06-27 12:41:38.084: D/AndroidRuntime(959): Shutting down VM
06-27 12:41:38.094: W/dalvikvm(959): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
06-27 12:41:38.144: E/AndroidRuntime(959): FATAL EXCEPTION: main
06-27 12:41:38.144: E/AndroidRuntime(959): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.citylife/com.example.citylife.ExpenseCalculator}: java.lang.NumberFormatException: Invalid int: ""
06-27 12:41:38.144: E/AndroidRuntime(959):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-27 12:41:38.144: E/AndroidRuntime(959):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-27 12:41:38.144: E/AndroidRuntime(959):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-27 12:41:38.144: E/AndroidRuntime(959):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-27 12:41:38.144: E/AndroidRuntime(959):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-27 12:41:38.144: E/AndroidRuntime(959):  at android.os.Looper.loop(Looper.java:137)
06-27 12:41:38.144: E/AndroidRuntime(959):  at android.app.ActivityThread.main(ActivityThread.java:5041)
06-27 12:41:38.144: E/AndroidRuntime(959):  at java.lang.reflect.Method.invokeNative(Native Method)
06-27 12:41:38.144: E/AndroidRuntime(959):  at java.lang.reflect.Method.invoke(Method.java:511)
06-27 12:41:38.144: E/AndroidRuntime(959):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-27 12:41:38.144: E/AndroidRuntime(959):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-27 12:41:38.144: E/AndroidRuntime(959):  at dalvik.system.NativeStart.main(Native Method)
06-27 12:41:38.144: E/AndroidRuntime(959): Caused by: java.lang.NumberFormatException: Invalid int: ""
06-27 12:41:38.144: E/AndroidRuntime(959):  at java.lang.Integer.invalidInt(Integer.java:138)
06-27 12:41:38.144: E/AndroidRuntime(959):  at java.lang.Integer.parseInt(Integer.java:359)
06-27 12:41:38.144: E/AndroidRuntime(959):  at java.lang.Integer.parseInt(Integer.java:332)
06-27 12:41:38.144: E/AndroidRuntime(959):  at com.example.citylife.ExpenseCalculator.onCreate(ExpenseCalculator.java:30)
06-27 12:41:38.144: E/AndroidRuntime(959):  at android.app.Activity.performCreate(Activity.java:5104)
06-27 12:41:38.144: E/AndroidRuntime(959):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-27 12:41:38.144: E/AndroidRuntime(959):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-27 12:41:38.144: E/AndroidRuntime(959):  ... 11 more
06-27 12:41:45.854: I/Process(959): Sending signal. PID: 959 SIG: 9
放置null检查器后

logcat:

06-27 13:39:10.275: D/gralloc_goldfish(2013): Emulator without GPU emulation detected.
06-27 13:43:57.694: D/dalvikvm(2013): GC_CONCURRENT freed 196K, 11% free 2598K/2920K, paused 7ms+34ms, total 259ms
06-27 13:43:57.844: D/AndroidRuntime(2013): Shutting down VM
06-27 13:43:57.844: W/dalvikvm(2013): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
06-27 13:43:57.914: E/AndroidRuntime(2013): FATAL EXCEPTION: main
06-27 13:43:57.914: E/AndroidRuntime(2013): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.citylife/com.example.citylife.ExpenseCalculator}: java.lang.NumberFormatException: Invalid int: ""
06-27 13:43:57.914: E/AndroidRuntime(2013):     at  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-27 13:43:57.914: E/AndroidRuntime(2013):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-27 13:43:57.914: E/AndroidRuntime(2013):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-27 13:43:57.914: E/AndroidRuntime(2013):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-27 13:43:57.914: E/AndroidRuntime(2013):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-27 13:43:57.914: E/AndroidRuntime(2013):     at android.os.Looper.loop(Looper.java:137)
06-27 13:43:57.914: E/AndroidRuntime(2013):     at android.app.ActivityThread.main(ActivityThread.java:5041)
06-27 13:43:57.914: E/AndroidRuntime(2013):     at java.lang.reflect.Method.invokeNative(Native Method)
06-27 13:43:57.914: E/AndroidRuntime(2013):     at java.lang.reflect.Method.invoke(Method.java:511)
06-27 13:43:57.914: E/AndroidRuntime(2013):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-27 13:43:57.914: E/AndroidRuntime(2013):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-27 13:43:57.914: E/AndroidRuntime(2013):     at dalvik.system.NativeStart.main(Native Method)
06-27 13:43:57.914: E/AndroidRuntime(2013): Caused by: java.lang.NumberFormatException: Invalid int: ""
06-27 13:43:57.914: E/AndroidRuntime(2013):     at java.lang.Integer.invalidInt(Integer.java:138)
06-27 13:43:57.914: E/AndroidRuntime(2013):     at java.lang.Integer.parseInt(Integer.java:359)
06-27 13:43:57.914: E/AndroidRuntime(2013):     at java.lang.Integer.parseInt(Integer.java:332)
06-27 13:43:57.914: E/AndroidRuntime(2013):     at com.example.citylife.ExpenseCalculator.onCreate(ExpenseCalculator.java:30)
06-27 13:43:57.914: E/AndroidRuntime(2013):     at android.app.Activity.performCreate(Activity.java:5104)
06-27 13:43:57.914: E/AndroidRuntime(2013):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-27 13:43:57.914: E/AndroidRuntime(2013):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-27 13:43:57.914: E/AndroidRuntime(2013):     ... 11 more
06-27 13:44:03.124: I/Process(2013): Sending signal. PID: 2013 SIG: 9
06-27 13:59:01.284: D/gralloc_goldfish(2367): Emulator without GPU emulation detected.
06-27 13:59:25.764: D/dalvikvm(2367): GC_CONCURRENT freed 169K, 11% free 2620K/2920K, paused 9ms+28ms, total 273ms
06-27 13:59:25.974: D/AndroidRuntime(2367): Shutting down VM
06-27 13:59:25.974: W/dalvikvm(2367): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
06-27 13:59:26.044: E/AndroidRuntime(2367): FATAL EXCEPTION: main
06-27 13:59:26.044: E/AndroidRuntime(2367): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.citylife/com.example.citylife.ExpenseCalculator}: java.lang.NumberFormatException: Invalid int: ""
06-27 13:59:26.044: E/AndroidRuntime(2367):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-27 13:59:26.044: E/AndroidRuntime(2367):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-27 13:59:26.044: E/AndroidRuntime(2367):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-27 13:59:26.044: E/AndroidRuntime(2367):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-27 13:59:26.044: E/AndroidRuntime(2367):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-27 13:59:26.044: E/AndroidRuntime(2367):     at android.os.Looper.loop(Looper.java:137)
06-27 13:59:26.044: E/AndroidRuntime(2367):     at android.app.ActivityThread.main(ActivityThread.java:5041)
06-27 13:59:26.044: E/AndroidRuntime(2367):     at java.lang.reflect.Method.invokeNative(Native Method)
06-27 13:59:26.044: E/AndroidRuntime(2367):     at java.lang.reflect.Method.invoke(Method.java:511)
06-27 13:59:26.044: E/AndroidRuntime(2367):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-27 13:59:26.044: E/AndroidRuntime(2367):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-27 13:59:26.044: E/AndroidRuntime(2367):     at dalvik.system.NativeStart.main(Native Method)
06-27 13:59:26.044: E/AndroidRuntime(2367): Caused by: java.lang.NumberFormatException: Invalid int: ""
06-27 13:59:26.044: E/AndroidRuntime(2367):     at java.lang.Integer.invalidInt(Integer.java:138)
06-27 13:59:26.044: E/AndroidRuntime(2367):     at java.lang.Integer.parseInt(Integer.java:359)
06-27 13:59:26.044: E/AndroidRuntime(2367):     at  java.lang.Integer.parseInt(Integer.java:332)
06-27 13:59:26.044: E/AndroidRuntime(2367):     at com.example.citylife.ExpenseCalculator.onCreate(ExpenseCalculator.java:31)
06-27 13:59:26.044: E/AndroidRuntime(2367):     at android.app.Activity.performCreate(Activity.java:5104)
06-27 13:59:26.044: E/AndroidRuntime(2367):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-27 13:59:26.044: E/AndroidRuntime(2367):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-27 13:59:26.044: E/AndroidRuntime(2367):     ... 11 more
06-27 13:59:31.525: I/Process(2367): Sending signal. PID: 2367 SIG: 9

new logcat:

06-27 14:16:09.574: D/gralloc_goldfish(2726): Emulator without GPU emulation detected.
06-27 14:18:48.964: D/dalvikvm(2726): GC_CONCURRENT freed 170K, 11% free 2611K/2912K, paused 10ms+30ms, total 209ms
06-27 14:18:49.036: D/AndroidRuntime(2726): Shutting down VM
06-27 14:18:49.044: W/dalvikvm(2726): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
06-27 14:18:49.114: E/AndroidRuntime(2726): FATAL EXCEPTION: main
06-27 14:18:49.114: E/AndroidRuntime(2726): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.citylife/com.example.citylife.ExpenseCalculator}: java.lang.NumberFormatException: Invalid int: ""
06-27 14:18:49.114: E/AndroidRuntime(2726):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-27 14:18:49.114: E/AndroidRuntime(2726):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-27 14:18:49.114: E/AndroidRuntime(2726):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-27 14:18:49.114: E/AndroidRuntime(2726):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-27 14:18:49.114: E/AndroidRuntime(2726):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-27 14:18:49.114: E/AndroidRuntime(2726):     at android.os.Looper.loop(Looper.java:137)
06-27 14:18:49.114: E/AndroidRuntime(2726):     at android.app.ActivityThread.main(ActivityThread.java:5041)
06-27 14:18:49.114: E/AndroidRuntime(2726):     at java.lang.reflect.Method.invokeNative(Native Method)
06-27 14:18:49.114: E/AndroidRuntime(2726):     at java.lang.reflect.Method.invoke(Method.java:511)
06-27 14:18:49.114: E/AndroidRuntime(2726):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-27 14:18:49.114: E/AndroidRuntime(2726):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-27 14:18:49.114: E/AndroidRuntime(2726):     at  dalvik.system.NativeStart.main(Native Method)
06-27 14:18:49.114: E/AndroidRuntime(2726): Caused by: java.lang.NumberFormatException: Invalid int: ""
06-27 14:18:49.114: E/AndroidRuntime(2726):     at java.lang.Integer.invalidInt(Integer.java:138)
06-27 14:18:49.114: E/AndroidRuntime(2726):     at java.lang.Integer.parseInt(Integer.java:359)
06-27 14:18:49.114: E/AndroidRuntime(2726):     at java.lang.Integer.parseInt(Integer.java:332)
06-27 14:18:49.114: E/AndroidRuntime(2726):     at com.example.citylife.ExpenseCalculator.onCreate(ExpenseCalculator.java:43)
06-27 14:18:49.114: E/AndroidRuntime(2726):     at android.app.Activity.performCreate(Activity.java:5104)
06-27 14:18:49.114: E/AndroidRuntime(2726):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-27 14:18:49.114: E/AndroidRuntime(2726):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-27 14:18:49.114: E/AndroidRuntime(2726):     ... 11 more
06-27 14:18:57.084: I/Process(2726): Sending signal. PID: 2726 SIG: 9

2 个答案:

答案 0 :(得分:3)

问题是当您从中检索值时,Edittext为null。您尚未将视图分配给它。

在分配视图后,只需移动代码即可检索值,如下面的代码所示。

public class ExpenseCalculator extends Activity {
EditText ed1,ed2,ed3,ed4,ed5,ed6,ed7;
Button btn1,btn2;
TextView tt1,tt2;
int a1,b2,c3,d4,e5,f6,g7; //these variables will be assigned 0
//Removed the code from here

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_expense_calculator);
    ed1=(EditText) findViewById(R.id.editText1);
    ed2=(EditText) findViewById(R.id.editText2);
    ed3=(EditText) findViewById(R.id.editText3);
    ed4=(EditText) findViewById(R.id.editText4);
    ed5=(EditText) findViewById(R.id.editText5);
    ed6=(EditText) findViewById(R.id.editText6);

    //added the code here after ed1,ed2,ed3,ed4,ed5,ed6 is assigned
    if(!ed1.getText().toString().equals(""))
    a1=Integer.parseInt(ed1.getText().toString());
    if(!ed2.getText().toString().equals(""))
    b2=Integer.parseInt(ed2.getText().toString());
    if(!ed3.getText().toString().equals(""))
    c3=Integer.parseInt(ed3.getText().toString());
    if(!ed4.getText().toString().equals(""))
    d4=Integer.parseInt(ed4.getText().toString());
    if(!ed5.getText().toString().equals(""))
    e5=Integer.parseInt(ed5.getText().toString());
    if(!ed6.getText().toString().equals(""))
    f6=Integer.parseInt(ed6.getText().toString());
    if(!ed7.getText().toString().equals(""))
    g7=Integer.parseInt(ed7.getText().toString());
    int total = a1 + b2 + c3 + d4 + e5 + f6;

答案 1 :(得分:1)

将int变量初始化为0。 即使你的edittext是空的,你也会有一个默认值。

或者在进行任何类型的数据操作之前先对值进行一些检查。

public class ExpenseCalculator extends Activity {
EditText ed1,ed2,ed3,ed4,ed5,ed6,ed7;
Button btn1,btn2;
TextView tt1,tt2;

//initialize to zero
int a1,b2,c3,d4,e5,f6,g7,total = 0;

/* this code below already takes input from edit text during intialization of code hence it's probably null

int a1=Integer.parseInt(ed1.getText().toString());
int b2=Integer.parseInt(ed2.getText().toString());
int c3=Integer.parseInt(ed3.getText().toString());
int d4=Integer.parseInt(ed4.getText().toString());
int e5=Integer.parseInt(ed5.getText().toString());
int f6=Integer.parseInt(ed6.getText().toString());
int g7=Integer.parseInt(ed7.getText().toString());
int total = a1 + b2 + c3 + d4 + e5 + f6;
*/

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_expense_calculator);
    ed1=(EditText) findViewById(R.id.editText1);
    ed2=(EditText) findViewById(R.id.editText2);
    ed3=(EditText) findViewById(R.id.editText3);
    ed4=(EditText) findViewById(R.id.editText4);
    ed5=(EditText) findViewById(R.id.editText5);
    ed6=(EditText) findViewById(R.id.editText6);
    ed7=(EditText) findViewById(R.id.editText7);
    btn1=(Button) findViewById(R.id.button1);
    btn2=(Button) findViewById(R.id.button2);
    tt1=(TextView) findViewById(R.id.textView7);
    tt2=(TextView) findViewById(R.id.TextView01);

    btn1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            tt1.setText(Integer.toString(total));

        }
    });
    btn2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
          //getting the input only when button clicked
          a1=Integer.parseInt(ed1.getText().toString());
          b2=Integer.parseInt(ed2.getText().toString());
          c3=Integer.parseInt(ed3.getText().toString());
          d4=Integer.parseInt(ed4.getText().toString());
          e5=Integer.parseInt(ed5.getText().toString());
          f6=Integer.parseInt(ed6.getText().toString());
          g7=Integer.parseInt(ed7.getText().toString());
          total = a1 + b2 + c3 + d4 + e5 + f6;

        if(g7>total)
            tt2.setText("Go for it !!");
        else
            tt2.setText("Out of budget :(");

        }
    });
}