我创建的按钮始终为null,当我尝试引用它时会导致空指针异常

时间:2018-04-16 19:20:10

标签: java android button nullpointerexception

我无法弄清楚如何正确初始化我正在尝试创建的按钮(按钮)。该按钮保持为null,这反过来导致它下面的行上出现NullPointerException。

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class SetCounter extends AppCompatActivity {

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

        //The code when the setButton is clicked.
        //Todo 1: define the setButton and create its setOnClickListener. inside it past the following code:-
          //On clicking the button the value of counterValueText should be sent to the MainActivity and it should be called.
            final Button button = (Button) findViewById(R.id.countButton);
            button.setOnClickListener(new View.OnClickListener() { //exception occurs on this line
                public void onClick(View v) {
                    EditText counterValueText = (EditText) findViewById(R.id.counterValueText);
                    int number = Integer.parseInt(counterValueText.getText().toString());
                    Intent activityChangeIntent = new Intent(SetCounter.this, MainActivity.class);

                    //Todo 2: use putExtra("Value",number); to pass values to the MainActivity.
                    activityChangeIntent.putExtra("Value", number);
                    startActivity(activityChangeIntent);
                }
            });
    }

}

发生错误
button.setOnClickListener(new View.OnClickListener() {

但我相信错误实际上就在这一行

final Button button = (Button) findViewById(R.id.countButton);

最后,我有另一个类MainActivity,我在其中声明了countButton,它看起来像这样

final Button countButton = (Button) findViewById(R.id.countButton);

添加了XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="***.*****.******.lab11skeletontwo.SetCounter">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_adddoc"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="mishra.sripath.wecareforuclient.Adddoc">


        <Button
            android:id="@+id/setButton"
            android:layout_width="88dp"
            android:layout_height="48dp"
            android:layout_marginStart="140dp"
            android:layout_marginLeft="140dp"
            android:layout_marginTop="400dp"
            android:text="Set" />

        <EditText
            android:id="@+id/counterValueText"
            android:layout_width="215dp"
            android:layout_height="46dp"
            android:ems="10"
            android:inputType="number"
            android:hint="Enter Value of Counter"
            android:layout_marginStart="70dp"
            android:layout_marginLeft="70dp"
            android:layout_marginTop="230dp" />
    </RelativeLayout>

</android.support.design.widget.CoordinatorLayout>

1 个答案:

答案 0 :(得分:1)

将按钮的ID从@ + id / setButton更改为xml中的@ + id / countButton

<Button
        android:id="@+id/setButton"
        android:layout_width="88dp"
        android:layout_height="48dp"
        android:layout_marginStart="140dp"
        android:layout_marginLeft="140dp"
        android:layout_marginTo