单击按钮后,为什么我的应用程序崩溃了?

时间:2012-07-24 03:02:45

标签: java android android-layout android-emulator android-widget

我有一个按钮id = button1,当点击它时会运行onEnterClick();功能但是每当我点击它时我的应用程序崩溃并说unexpectedly quit。我将在下面提供java文件和xml,非常感谢任何帮助。

package tip.calculator;

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

public class MainActivity extends Activity {

    final EditText myEditField = (EditText) findViewById(R.id.percentText);
    final EditText myEditField2 = (EditText) findViewById(R.id.billText);


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public void onEnterClick(View view){   //This Function Runs When The Button is clicked

        TextView textView;

        textView = 
                (TextView) findViewById(R.id.textView1);

        final EditText myEditField = (EditText) findViewById(R.id.percentText);
        final EditText myEditField2 = (EditText) findViewById(R.id.billText);

        int percentage;
        int percentageInp;
        int billAmount;
        int output; 
        String output1;

        percentageInp = Integer.parseInt(myEditField.getText().toString());
        billAmount = Integer.parseInt(myEditField2.getText().toString());

        percentage = percentageInp /100;
        output = (billAmount * percentage);
        output1 = Integer.toString(output);

        textView.setText(output1);
    }


}
那是Java ^^ 这是xml:

<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:onClick="onEnterClick" >

    <EditText
        android:id="@+id/billText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:ems="10"
        android:hint="Bill Amount"
        android:inputType="numberDecimal|textAutoComplete" />

    <EditText
        android:id="@+id/percentText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/billText"
        android:layout_below="@+id/billText"
        android:ems="10"
        android:hint="Percent"
        android:inputType="number" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/percentText"
        android:layout_centerHorizontal="true"
        android:onClick="onEnterClick"
        android:text="Calculate" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="287dp"
        android:text="Solution Will Appear Here:"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Log Cat:

07-24 03:12:26.563: D/AndroidRuntime(412): Shutting down VM
07-24 03:12:26.563: W/dalvikvm(412): threadid=1: thread exiting with uncaught exception (group=0x40014760)
07-24 03:12:26.763: E/AndroidRuntime(412): FATAL EXCEPTION: main
07-24 03:12:26.763: E/AndroidRuntime(412): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{tip.calculator/tip.calculator.MainActivity}: java.lang.NullPointerException
07-24 03:12:26.763: E/AndroidRuntime(412):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1739)
07-24 03:12:26.763: E/AndroidRuntime(412):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
07-24 03:12:26.763: E/AndroidRuntime(412):  at android.app.ActivityThread.access$500(ActivityThread.java:122)
07-24 03:12:26.763: E/AndroidRuntime(412):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
07-24 03:12:26.763: E/AndroidRuntime(412):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-24 03:12:26.763: E/AndroidRuntime(412):  at android.os.Looper.loop(Looper.java:132)
07-24 03:12:26.763: E/AndroidRuntime(412):  at android.app.ActivityThread.main(ActivityThread.java:4123)
07-24 03:12:26.763: E/AndroidRuntime(412):  at java.lang.reflect.Method.invokeNative(Native Method)
07-24 03:12:26.763: E/AndroidRuntime(412):  at java.lang.reflect.Method.invoke(Method.java:491)
07-24 03:12:26.763: E/AndroidRuntime(412):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
07-24 03:12:26.763: E/AndroidRuntime(412):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
07-24 03:12:26.763: E/AndroidRuntime(412):  at dalvik.system.NativeStart.main(Native Method)
07-24 03:12:26.763: E/AndroidRuntime(412): Caused by: java.lang.NullPointerException
07-24 03:12:26.763: E/AndroidRuntime(412):  at android.app.Activity.findViewById(Activity.java:1745)
07-24 03:12:26.763: E/AndroidRuntime(412):  at tip.calculator.MainActivity.<init>(MainActivity.java:12)
07-24 03:12:26.763: E/AndroidRuntime(412):  at java.lang.Class.newInstanceImpl(Native Method)
07-24 03:12:26.763: E/AndroidRuntime(412):  at java.lang.Class.newInstance(Class.java:1301)
07-24 03:12:26.763: E/AndroidRuntime(412):  at android.app.Instrumentation.newActivity(Instrumentation.java:1022)
07-24 03:12:26.763: E/AndroidRuntime(412):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1730)
07-24 03:12:26.763: E/AndroidRuntime(412):  ... 11 more
07-24 03:12:34.871: I/Process(412): Sending signal. PID: 412 SIG: 9
07-24 03:15:54.102: D/AndroidRuntime(453): Shutting down VM
07-24 03:15:54.216: W/dalvikvm(453): threadid=1: thread exiting with uncaught exception (group=0x40014760)
07-24 03:15:54.381: E/AndroidRuntime(453): FATAL EXCEPTION: main
07-24 03:15:54.381: E/AndroidRuntime(453): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{tip.calculator/tip.calculator.MainActivity}: java.lang.NullPointerException
07-24 03:15:54.381: E/AndroidRuntime(453):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1739)
07-24 03:15:54.381: E/AndroidRuntime(453):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
07-24 03:15:54.381: E/AndroidRuntime(453):  at android.app.ActivityThread.access$500(ActivityThread.java:122)
07-24 03:15:54.381: E/AndroidRuntime(453):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
07-24 03:15:54.381: E/AndroidRuntime(453):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-24 03:15:54.381: E/AndroidRuntime(453):  at android.os.Looper.loop(Looper.java:132)
07-24 03:15:54.381: E/AndroidRuntime(453):  at android.app.ActivityThread.main(ActivityThread.java:4123)
07-24 03:15:54.381: E/AndroidRuntime(453):  at java.lang.reflect.Method.invokeNative(Native Method)
07-24 03:15:54.381: E/AndroidRuntime(453):  at java.lang.reflect.Method.invoke(Method.java:491)
07-24 03:15:54.381: E/AndroidRuntime(453):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
07-24 03:15:54.381: E/AndroidRuntime(453):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
07-24 03:15:54.381: E/AndroidRuntime(453):  at dalvik.system.NativeStart.main(Native Method)
07-24 03:15:54.381: E/AndroidRuntime(453): Caused by: java.lang.NullPointerException
07-24 03:15:54.381: E/AndroidRuntime(453):  at android.app.Activity.findViewById(Activity.java:1745)
07-24 03:15:54.381: E/AndroidRuntime(453):  at tip.calculator.MainActivity.<init>(MainActivity.java:12)
07-24 03:15:54.381: E/AndroidRuntime(453):  at java.lang.Class.newInstanceImpl(Native Method)
07-24 03:15:54.381: E/AndroidRuntime(453):  at java.lang.Class.newInstance(Class.java:1301)
07-24 03:15:54.381: E/AndroidRuntime(453):  at android.app.Instrumentation.newActivity(Instrumentation.java:1022)
07-24 03:15:54.381: E/AndroidRuntime(453):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1730)
07-24 03:15:54.381: E/AndroidRuntime(453):  ... 11 more

1 个答案:

答案 0 :(得分:0)

在调用setContentView(R.layout.activity_main);部分之前,请不要初始化UI元素。

将您的代码更改为

public class MainActivity extends Activity 
{
private EditText myEditField;
private EditText myEditField2;
private Button enter;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myEditField = (EditText) findViewById(R.id.percentText);
    myEditField2 = (EditText) findViewById(R.id.billText);
    enter = (Button)findViewById(R.id.button1);

    enter.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            TextView textView;

            textView = (TextView) findViewById(R.id.textView1);

            final EditText myEditField = (EditText) findViewById(R.id.percentText);
            final EditText myEditField2 = (EditText) findViewById(R.id.billText);

            float percentage;//=======> declate as float
            int percentageInp;
            int billAmount;
            int output; 
            String output1;

            percentageInp = Integer.parseInt(myEditField.getText().toString());
            billAmount = Integer.parseInt(myEditField2.getText().toString());

            System.out.println(percentageInp+" "+billAmount);

            percentage = ((float)percentageInp /100);//=====>check here

            output = (int)(billAmount * percentage);
            output1 = Integer.toString(output);

            textView.setText(output1);

        }
    });
}
}