Bug Calculator Program for Android

2019-09-08 17:06发布

问题:

I've finished coding the simple program but when I try to run it on the emulator, it stops and I see the following error in my LogCat. My MainActivity.java and activity_main.xml will also be provided below.

07-23 07:43:17.256: D/AndroidRuntime(1259): Shutting down VM
07-23 07:43:17.256: W/dalvikvm(1259): threadid=1: thread exiting with uncaught exception (group=0xb3ad7ba8)
07-23 07:43:17.296: E/AndroidRuntime(1259): FATAL EXCEPTION: main
07-23 07:43:17.296: E/AndroidRuntime(1259): Process: com.example.owncode, PID: 1259
07-23 07:43:17.296: E/AndroidRuntime(1259): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.owncode/com.example.owncode.MainActivity}: java.lang.NullPointerException
07-23 07:43:17.296: E/AndroidRuntime(1259):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
07-23 07:43:17.296: E/AndroidRuntime(1259):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-23 07:43:17.296: E/AndroidRuntime(1259):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-23 07:43:17.296: E/AndroidRuntime(1259):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-23 07:43:17.296: E/AndroidRuntime(1259):     at android.os.Handler.dispatchMessage(Handler.java:102)
07-23 07:43:17.296: E/AndroidRuntime(1259):     at android.os.Looper.loop(Looper.java:136)
07-23 07:43:17.296: E/AndroidRuntime(1259):     at android.app.ActivityThread.main(ActivityThread.java:5017)
07-23 07:43:17.296: E/AndroidRuntime(1259):     at java.lang.reflect.Method.invokeNative(Native Method)
07-23 07:43:17.296: E/AndroidRuntime(1259):     at java.lang.reflect.Method.invoke(Method.java:515)
07-23 07:43:17.296: E/AndroidRuntime(1259):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-23 07:43:17.296: E/AndroidRuntime(1259):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-23 07:43:17.296: E/AndroidRuntime(1259):     at dalvik.system.NativeStart.main(Native Method)
07-23 07:43:17.296: E/AndroidRuntime(1259): Caused by: java.lang.NullPointerException
07-23 07:43:17.296: E/AndroidRuntime(1259):     at android.app.Activity.findViewById(Activity.java:1884)
07-23 07:43:17.296: E/AndroidRuntime(1259):     at com.example.owncode.MainActivity.<init>(MainActivity.java:11)
07-23 07:43:17.296: E/AndroidRuntime(1259):     at java.lang.Class.newInstanceImpl(Native Method)
07-23 07:43:17.296: E/AndroidRuntime(1259):     at java.lang.Class.newInstance(Class.java:1208)
07-23 07:43:17.296: E/AndroidRuntime(1259):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
07-23 07:43:17.296: E/AndroidRuntime(1259):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
07-23 07:43:17.296: E/AndroidRuntime(1259):     ... 11 more
07-23 07:43:27.146: I/Process(1259): Sending signal. PID: 1259 SIG: 9

Here's my JAVA code.

package com.example.owncode;

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

public class MainActivity extends Activity {
EditText calculatorText = (EditText) findViewById(R.id.calculatorText);

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


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

public void button_one_click(View view){
    calculatorText.append("1");
}

public void button_two_click(View view){
    calculatorText.append("2");
}
public void button_three_click(View view){
    calculatorText.append("3");
}
public void button_four_click(View view){
    calculatorText.append("4");
}
public void button_five_click(View view){
    calculatorText.append("5");
}
public void button_six_click(View view){
    calculatorText.append("6");
}
public void button_seven_click(View view){
    calculatorText.append("7");
}
public void button_eight_click(View view){
    calculatorText.append("8");
}
public void button_nine_click(View view){
    calculatorText.append("9");
}
public void button_zero_click(View view){
    calculatorText.append("0");
}
public void button_plus_click(View view){
    calculatorText.append("+");
}
public void button_minus_click(View view){
    calculatorText.append("-");
}

}

Here's my XML code.

<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:orientation="horizontal" >

<EditText
android:id="@+id/calculatorText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="@string/calculatorText_text" />

<Button
android:id="@+id/button_plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button_minus"
android:layout_alignBottom="@+id/button_minus"
android:layout_alignParentLeft="true"
android:hint="@string/button_plus"
android:onClick="button_plus_click" />

<Button
android:id="@+id/button_zero"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button_plus"
android:layout_alignBottom="@+id/button_plus"
android:layout_toRightOf="@+id/button_plus"
android:hint="@string/button_zero"
android:onClick="button_zero_click" />

<Button
android:id="@+id/button_minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="135dp"
android:hint="@string/button_minus"
android:onClick="button_minus_click" />

<Button
android:id="@+id/button_nine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button_minus"
android:layout_alignLeft="@+id/button_minus"
android:hint="@string/button_nine"
android:onClick="button_nine_click" />

<Button
android:id="@+id/button_eight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button_zero"
android:layout_toLeftOf="@+id/button_minus"
android:hint="@string/button_eight"
android:onClick="button_eight_click" />

<Button
android:id="@+id/button_seven"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button_eight"
android:layout_alignBottom="@+id/button_eight"
android:layout_toLeftOf="@+id/button_eight"
android:hint="@string/button_seven"
android:onClick="button_seven_click" />

<Button
android:id="@+id/button_six"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button_nine"
android:layout_toRightOf="@+id/button_eight"
android:hint="@string/button_six"
android:onClick="button_six_click" />

<Button
android:id="@+id/button_five"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button_six"
android:layout_alignBottom="@+id/button_six"
android:layout_toRightOf="@+id/button_seven"
android:hint="@string/button_five"
android:onClick="button_five_click" />

<Button
android:id="@+id/button_four"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button_five"
android:layout_alignBottom="@+id/button_five"
android:layout_toLeftOf="@+id/button_five"
android:hint="@string/button_four"
android:onClick="button_four_click" />

<Button
android:id="@+id/button_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button_six"
android:layout_toRightOf="@+id/button_five"
android:hint="@string/button_three"
android:onClick="button_three_click" />

<Button
android:id="@+id/button_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button_three"
android:layout_alignBottom="@+id/button_three"
android:layout_toRightOf="@+id/button_four"
android:hint="@string/button_two"
android:onClick="button_two_click" />

<Button
android:id="@+id/button_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button_two"
android:layout_alignBottom="@+id/button_two"
android:layout_toLeftOf="@+id/button_two"
android:hint="@string/button_one"
android:onClick="button_one_click" />

</RelativeLayout>

Please help.

回答1:

Shift the following inside onCreate() below setContentView():

EditText calculatorText = (EditText) findViewById(R.id.calculatorText);  

The activity cannot identify where to "find" the editText by id outside onCreate before setContentView.



回答2:

Break up your code like this:

public class MainActivity extends Activity {
EditText calculatorText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    calculatorText = (EditText) findViewById(R.id.calculatorText);
}

The problem with your code is that you first try to find a View, inside of your Content View (R.layout.activity_main) but you haven't set the content view of your activity at the point that you tried to instantiate that View!



回答3:

you are calling findviewById before you have even set the layout!

this line

EditText calculatorText = (EditText) findViewById(R.id.calculatorText);

should be :

EditText calculatorText;

and then after you :

setContentView(R.layout.activity_main);

write :

calculatorText = (EditText) findViewById(R.id.calculatorText);