简体   繁体   中英

I can't figure out why this piece of code does not work (null pointer exception)

I am new to Android development and am trying to make a simple calculator. But when I run the app, it says it stopped working. Here is the Java code and XML code.

    TextView txtView1 = (TextView)findViewById(R.id.textView1);
    Button Button01;
    Button Button02;
    Button Button03;
    Button Button04;
    Button Button05;
    Button Button06;
    Button Button07;
    Button Button08;

    public void onClickButton (View view)
    {
        if (view == findViewById(R.id.Button01)){
            txtView1.setText("7");
        } else if (view == findViewById(R.id.Button02)){
            txtView1.setText("8");
        } else if (view == findViewById(R.id.Button03)){
            txtView1.setText("9");
        } else if (view == findViewById(R.id.Button04)){
            txtView1.setText("/");
        } else if (view == findViewById(R.id.Button05)){
            txtView1.setText("4");
        } else if (view == findViewById(R.id.Button06)){
            txtView1.setText("5");
        } else if (view == findViewById(R.id.Button07)){
            txtView1.setText("6");
        } else if (view == findViewById(R.id.Button08)){
            txtView1.setText("+");
        } else if (view == findViewById(R.id.Button09)){
            txtView1.setText("1");
        } else if (view == findViewById(R.id.Button10)){
            txtView1.setText("2");
        } else if (view == findViewById(R.id.Button11)){
            txtView1.setText("3");
        } else if (view == findViewById(R.id.Button12)){
            txtView1.setText("-");
        }
    }

And the 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"
    tools:context=".MainActivity"
    android:baselineAligned="false"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2.5"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            android:onClick="onClickButton" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            android:onClick="onClickButton" />

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            android:onClick="onClickButton" />

        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="x"
            android:onClick="onClickButton" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <Button
            android:id="@+id/Button01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="7"
            android:onClick="onClickButton" />

        <Button
            android:id="@+id/Button02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="8"
            android:onClick="onClickButton" />

        <Button
            android:id="@+id/Button03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="9"
            android:onClick="onClickButton" />

        <Button
            android:id="@+id/Button04"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="/"
            android:onClick="onClickButton" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <Button
            android:id="@+id/Button05"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="4"
            android:onClick="onClickButton" />

        <Button
            android:id="@+id/Button06"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="5"
            android:onClick="onClickButton" />

        <Button
            android:id="@+id/Button07"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="6"
            android:onClick="onClickButton" />

        <Button
            android:id="@+id/Button08"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+"
            android:onClick="onClickButton" />
    </LinearLayout>

</LinearLayout>

When I run this, I see a null-pointer exception in the LogCat . But I went through all the variables and made sure they were all there and referenced properly.

The LogCat:

12-08 12:12:20.255: D/AndroidRuntime(5954): Shutting down VM
12-08 12:12:20.255: W/dalvikvm(5954): threadid=1: thread exiting with uncaught exception (group=0x41517b90)
12-08 12:12:20.265: I/Process(5954): Sending signal. PID: 5954 SIG: 9
12-08 12:12:20.265: E/AndroidRuntime(5954): FATAL EXCEPTION: main
12-08 12:12:20.265: E/AndroidRuntime(5954): Process: com.example.mysecondapp, PID: 5954
12-08 12:12:20.265: E/AndroidRuntime(5954): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.mysecondapp/com.example.mysecondapp.MainActivity}: java.lang.NullPointerException
12-08 12:12:20.265: E/AndroidRuntime(5954):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2102)
12-08 12:12:20.265: E/AndroidRuntime(5954):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
12-08 12:12:20.265: E/AndroidRuntime(5954):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
12-08 12:12:20.265: E/AndroidRuntime(5954):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
12-08 12:12:20.265: E/AndroidRuntime(5954):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-08 12:12:20.265: E/AndroidRuntime(5954):     at android.os.Looper.loop(Looper.java:137)
12-08 12:12:20.265: E/AndroidRuntime(5954):     at android.app.ActivityThread.main(ActivityThread.java:4998)
12-08 12:12:20.265: E/AndroidRuntime(5954):     at java.lang.reflect.Method.invokeNative(Native Method)
12-08 12:12:20.265: E/AndroidRuntime(5954):     at java.lang.reflect.Method.invoke(Method.java:515)
12-08 12:12:20.265: E/AndroidRuntime(5954):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-08 12:12:20.265: E/AndroidRuntime(5954):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-08 12:12:20.265: E/AndroidRuntime(5954):     at dalvik.system.NativeStart.main(Native Method)
12-08 12:12:20.265: E/AndroidRuntime(5954): Caused by: java.lang.NullPointerException
12-08 12:12:20.265: E/AndroidRuntime(5954):     at android.app.Activity.findViewById(Activity.java:1883)
12-08 12:12:20.265: E/AndroidRuntime(5954):     at com.example.mysecondapp.MainActivity.<init>(MainActivity.java:13)
12-08 12:12:20.265: E/AndroidRuntime(5954):     at java.lang.Class.newInstanceImpl(Native Method)
12-08 12:12:20.265: E/AndroidRuntime(5954):     at java.lang.Class.newInstance(Class.java:1208)
12-08 12:12:20.265: E/AndroidRuntime(5954):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
12-08 12:12:20.265: E/AndroidRuntime(5954):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2093)
12-08 12:12:20.265: E/AndroidRuntime(5954):     ... 11 more

Do such TextView txtView1=(TextView)findViewById(R.id.textView1); things after your setContentView() , maybe in your onCreate() function.

You are trying to get a TextView from an unimported view.

I mean like this:

TextView txtView1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout);
    txtView1=(TextView)findViewById(R.id.textView1);
}

Put txtView1=(TextView)findViewById(R.id.textView1); in onCreate method like this

@Override
protected void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.myxml);
    txtView1=(TextView)findViewById(R.id.textView1);
    super.onCreate(savedInstanceState);
}

You can not call any findViewById if you did not set content view.

I wonder how your code works even. The below snippet is absolutely wrong.

if (view == findViewById(R.id.Button01)) {
    txtView1.setText("7");
} else if (view == findViewById(R.id.Button02)) {
    txtView1.setText("8");
}

Your buttons have ids like button1, button2,... And you are referring them as R.id.Button01 and so on.

You should check it in this way:

if (view.getId() == R.id.button1){
    txtView1.setText("1");
} else if (view.getId() == R.id.button2){
    txtView1.setText("2");
}

And you need to refer your textview only after setting the contentview in your onCreate() and not as class member as a whole.

As the error says, there's a mistake in . First, you had a "findViewById(R.id. B utton1)", even though the IDs were " b utton1" and so. And you haven't included the onCreate() method, you need to set the layout with setContentView(R.id.your_layout)there. After that, you can initialize variables inside all methods, including onCreate(), and access them inside the onButtonClicked() or other methods.

I hope I helped you with my summary of other answers.

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