致命异常:无法启动活动组件信息

时间:2014-06-20 17:38:02

标签: android

我是Android应用程序开发的新手,现在我正在尝试制作一个简单的计算器应用程序。编译器没有检测到任何错误,并且成功下载到手机,但是在运行时它会收到一堆运行时错误。

   public class MainActivity extends Activity {

Button one; 
Button two;
Button three; 
Button four;
Button five;
Button six;
Button seven;
Button eight;
Button nine;
Button add;
Button sub;
Button multiply;
Button divide;
Button clear;
Button equals;
String calculateNumber;
TextView textRead;
boolean ADD = false;
boolean SUB = false;
boolean MULTIPLY = false;
boolean DIVIDE = false;

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

    //references all the objects within the XML file
    one = (Button)findViewById(R.id.One);
    two = (Button)findViewById(R.id.Two);
    three = (Button)findViewById(R.id.Three);
    four = (Button)findViewById(R.id.Four);
    five = (Button)findViewById(R.id.Five);
    six= (Button)findViewById(R.id.Six);
    seven = (Button)findViewById(R.id.Seven);
    eight= (Button)findViewById(R.id.Eight);
    nine = (Button)findViewById(R.id.Nine);
    add= (Button)findViewById(R.id.Add);
    sub = (Button)findViewById(R.id.Sub);
    multiply = (Button)findViewById(R.id.Multiply);
    divide= (Button)findViewById(R.id.Divide);
    equals = (Button)findViewById(R.id.Equal);
    textRead = (TextView)findViewById(R.id.Numbers);

    //Action listeners for each button to add it to the string
    one.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            calculateNumber = calculateNumber + "1";
            textRead.setText(calculateNumber);
        } 
    });

    two.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            calculateNumber = calculateNumber + "2";
            textRead.setText(calculateNumber);
        }
    });


    three.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            calculateNumber = calculateNumber + "3";
            textRead.setText(calculateNumber);
        }
    });

    four.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            calculateNumber = calculateNumber + "4";
            textRead.setText(calculateNumber);
        }
    });

    five.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            calculateNumber = calculateNumber + "5";
            textRead.setText(calculateNumber);
        }
    });

    six.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                calculateNumber = calculateNumber + "6";
                textRead.setText(calculateNumber);
            }
         });

    seven.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                calculateNumber = calculateNumber + "7";
                textRead.setText(calculateNumber);
            }
         });

    eight.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                calculateNumber = calculateNumber + "8";
                textRead.setText(calculateNumber);
            }
         });

    nine.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                calculateNumber = calculateNumber + "9";
                textRead.setText(calculateNumber);
            }
         });

    add.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                calculateNumber = calculateNumber + " + ";
                ADD = true;
                textRead.setText(calculateNumber);
            }
         });

    sub.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                calculateNumber = calculateNumber + " - ";
                SUB = true;
                textRead.setText(calculateNumber);
            }
         });

    multiply.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                calculateNumber = calculateNumber + " x ";
                MULTIPLY = true;
                textRead.setText(calculateNumber);
            }
         });

    divide.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                calculateNumber = calculateNumber + " / ";
                DIVIDE = true;
                textRead.setText(calculateNumber);
            }
         });

    clear.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                calculateNumber = "";
                ADD = false;
                SUB = false;
                MULTIPLY = false;
                DIVIDE = false;
                textRead.setText(calculateNumber);
            }
         });

    equals.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                //splits the one large string into two strings that can be parsable into Ints
                String[] bothNumbers = calculateNumber.split("\\+");

                double numberOne = 0;
                double numberTwo = 0;

                numberOne =  Double.parseDouble(bothNumbers[0]); 
                numberTwo =  Double.parseDouble(bothNumbers[1]); 

                if(ADD = true)
                    {
                    calculateNumber = "" + (numberOne+numberTwo);
                    ADD = false;
                    }

                else if(SUB = true)
                    {
                    calculateNumber = "" + (numberOne - numberTwo);
                    SUB = false;
                    }

                else if (MULTIPLY = true)
                    {
                    calculateNumber = "" + (numberOne*numberTwo);
                    MULTIPLY = false;
                    }

                else if (DIVIDE = true)
                    {
                    calculateNumber = "" + (numberOne/numberTwo);
                    DIVIDE = false;
                    }
                textRead.setText(calculateNumber);
            }
         });
}     

}

这是我的一个Java文件,其中动作侦听器用于按钮。它应该没有任何问题,因为只有在按下按钮时才会调用它们。

    <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.cakculator.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>

以下是eclipse给我的错误

06-20 10:48:16.678: E/AndroidRuntime(25231): FATAL EXCEPTION: main 
06-20 10:48:16.678: E/AndroidRuntime(25231): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cakculator/com.example.cakculator.MainActivity}: java.lang.NullPointerException 
06-20 10:48:16.678: E/AndroidRuntime(25231): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967) 
06-20 10:48:16.678: E/AndroidRuntime(25231):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992) 
06-20 10:48:16.678: E/AndroidRuntime(25231):    at android.app.ActivityThread.access$600(ActivityThread.java:127) 
06-20 10:48:16.678: E/AndroidRuntime(25231):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158) 
06-20 10:48:16.678: E/AndroidRuntime(25231):    at android.os.Handler.dispatchMessage(Handler.java:99) 
06-20 10:48:16.678: E/AndroidRuntime(25231):    at android.os.Looper.loop(Looper.java:137) 
06-20 10:48:16.678: E/AndroidRuntime(25231):    at android.app.ActivityThread.main(ActivityThread.java:4511) 
06-20 10:48:16.678: E/AndroidRuntime(25231):    at java.lang.reflect.Method.invokeNative(Native Method) 
06-20 10:48:16.678: E/AndroidRuntime(25231):    at java.lang.reflect.Method.invoke(Method.java:511) 
06-20 10:48:16.678: E/AndroidRuntime(25231):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:976) 
06-20 10:48:16.678: E/AndroidRuntime(25231):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:743) 
06-20 10:48:16.678: E/AndroidRuntime(25231):    at dalvik.system.NativeStart.main(Native Method) 
06-20 10:48:16.678: E/AndroidRuntime(25231): Caused by: java.lang.NullPointerException 
06-20 10:48:16.678: E/AndroidRuntime(25231):    at com.example.cakculator.MainActivity.onCreate(MainActivity.java:152) 
06-20 10:48:16.678: E/AndroidRuntime(25231):    at android.app.Activity.performCreate(Activity.java:4486) 
06-20 10:48:16.678: E/AndroidRuntime(25231):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052) 
06-20 10:48:16.678: E/AndroidRuntime(25231):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931) 
06-20 10:48:16.678: E/AndroidRuntime(25231):    ... 11 more

如果你们能帮助我找到导致错误的事情,那就非常感激了!

1 个答案:

答案 0 :(得分:4)

Button clear未设置 - &gt; NPE。