Java 获取活动中的按钮文本时出现NullPointerException

Java 获取活动中的按钮文本时出现NullPointerException,java,android,Java,Android,我正在尝试编写一个简单的应用程序,当点击布局中的一个按钮时输出一个祝酒词。我觉得这是一个非常简单的修复,但无法让它正常运行 public class MainActivity extends ActionBarActivity implements View.OnClickListener{ private Button btnSpotifyApp, btnScoresApp, btnLibraryApp, btnBuildItBigger, btnXYZReader, btnCapstone

我正在尝试编写一个简单的应用程序,当点击布局中的一个按钮时输出一个祝酒词。我觉得这是一个非常简单的修复,但无法让它正常运行

public class MainActivity extends ActionBarActivity implements View.OnClickListener{

private Button btnSpotifyApp, btnScoresApp, btnLibraryApp, btnBuildItBigger, btnXYZReader, btnCapstone;
private Toast mToast;

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

    Button btnSpotifyapp = (Button)findViewById(R.id.btnSpotifyApp);
    Button btnScoresApp = (Button)findViewById(R.id.btnScoresApp);
    Button btnLibraryApp = (Button)findViewById(R.id.btnLibraryApp);
    Button btnBuildItBigger = (Button)findViewById(R.id.btnBuildItBigger);
    Button btnXYZReader = (Button)findViewById(R.id.btnXYZReader);
    Button btnCapstone = (Button)findViewById(R.id.btnCapstone);

    btnSpotifyapp.setOnClickListener(this);
    btnScoresApp.setOnClickListener(this);
    btnLibraryApp.setOnClickListener(this);
    btnBuildItBigger.setOnClickListener(this);
    btnXYZReader.setOnClickListener(this);
    btnCapstone.setOnClickListener(this);
}

/**
 * Called when a view has been clicked.
 *
 * @param v The view that was clicked.
 */
@Override
public void onClick(View v) {
    switch (v.getId()) {
        //SignIn Button Clicked
        case R.id.btnSpotifyApp:
            showToast(btnSpotifyApp.getText().toString());
            break;
        case R.id.btnScoresApp:
            showToast(btnScoresApp.getText().toString());
            break;
        case R.id.btnLibraryApp:
            showToast(btnLibraryApp.getText().toString());
            break;
        case R.id.btnBuildItBigger:
            showToast(btnBuildItBigger.getText().toString());
            break;
        case R.id.btnXYZReader:
            showToast(btnXYZReader.getText().toString());
            break;
        case R.id.btnCapstone:
            showToast(btnCapstone.getText().toString());
            break;
    }
}
以下是它的完整日志:

Process: com.app, PID: 6964
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.Button.getText()' on a null object reference
            at com.app.MainActivity.onClick(MainActivity.java:59)
            at android.view.View.performClick(View.java:4780)
            at android.view.View$PerformClick.run(View.java:19866)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

使用在onCreate()方法之前已经定义的按钮变量。您的onCreate()方法应该如下所示

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

btnSpotifyapp = (Button)findViewById(R.id.btnSpotifyApp);
btnScoresApp = (Button)findViewById(R.id.btnScoresApp);
btnLibraryApp = (Button)findViewById(R.id.btnLibraryApp);
btnBuildItBigger = (Button)findViewById(R.id.btnBuildItBigger);
btnXYZReader = (Button)findViewById(R.id.btnXYZReader);
btnCapstone = (Button)findViewById(R.id.btnCapstone);

btnSpotifyapp.setOnClickListener(this);
btnScoresApp.setOnClickListener(this);
btnLibraryApp.setOnClickListener(this);
btnBuildItBigger.setOnClickListener(this);
btnXYZReader.setOnClickListener(this);
btnCapstone.setOnClickListener(this);
}

使用在onCreate()方法之前已经定义的按钮变量。您的onCreate()方法应该如下所示

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

btnSpotifyapp = (Button)findViewById(R.id.btnSpotifyApp);
btnScoresApp = (Button)findViewById(R.id.btnScoresApp);
btnLibraryApp = (Button)findViewById(R.id.btnLibraryApp);
btnBuildItBigger = (Button)findViewById(R.id.btnBuildItBigger);
btnXYZReader = (Button)findViewById(R.id.btnXYZReader);
btnCapstone = (Button)findViewById(R.id.btnCapstone);

btnSpotifyapp.setOnClickListener(this);
btnScoresApp.setOnClickListener(this);
btnLibraryApp.setOnClickListener(this);
btnBuildItBigger.setOnClickListener(this);
btnXYZReader.setOnClickListener(this);
btnCapstone.setOnClickListener(this);
}

检查OnCreate()方法内的变量范围按钮,您不是在初始化类私有按钮对象字段,而是在OnCreate()的范围内创建新按钮

你应该这样做:

    this.btnSpotifyapp = (Button)findViewById(R.id.btnSpotifyApp);

检查OnCreate()方法内的变量范围按钮,您不是在初始化类私有按钮对象字段,而是在OnCreate()的范围内创建新按钮

你应该这样做:

    this.btnSpotifyapp = (Button)findViewById(R.id.btnSpotifyApp);

您正在重新声明onCreate中的所有按钮,因此您的类成员未初始化,因此它们为null,我猜是btnSpotifyApp第一次导致它

另外-尝试更正以下内容的拼写:

private Button btnSpotifyApp
与之相反:

btnSpotifyapp.setOnClickListener(this);
试试这个:

public class MainActivity extends ActionBarActivity implements View.OnClickListener{

private Button btnSpotifyApp, btnScoresApp, btnLibraryApp, btnBuildItBigger, btnXYZReader, btnCapstone;
private Toast mToast;

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

    this.btnSpotifyApp = (Button)findViewById(R.id.btnSpotifyApp);
    this.btnScoresApp = (Button)findViewById(R.id.btnScoresApp);
    this.btnLibraryApp = (Button)findViewById(R.id.btnLibraryApp);
    this.btnBuildItBigger = (Button)findViewById(R.id.btnBuildItBigger);
    this.btnXYZReader = (Button)findViewById(R.id.btnXYZReader);
    this.btnCapstone = (Button)findViewById(R.id.btnCapstone);

    btnSpotifyApp.setOnClickListener(this);
    btnScoresApp.setOnClickListener(this);
    btnLibraryApp.setOnClickListener(this);
    btnBuildItBigger.setOnClickListener(this);
    btnXYZReader.setOnClickListener(this);
    btnCapstone.setOnClickListener(this);
}

/**
 * Called when a view has been clicked.
 *
 * @param v The view that was clicked.
 */
@Override
public void onClick(View v) {
    switch (v.getId()) {
        //SignIn Button Clicked
        case R.id.btnSpotifyApp:
            showToast(this.btnSpotifyApp.getText().toString());
            break;
        case R.id.btnScoresApp:
            showToast(this.btnScoresApp.getText().toString());
            break;
        case R.id.btnLibraryApp:
            showToast(this.btnLibraryApp.getText().toString());
            break;
        case R.id.btnBuildItBigger:
            showToast(this.btnBuildItBigger.getText().toString());
            break;
        case R.id.btnXYZReader:
            showToast(this.btnXYZReader.getText().toString());
            break;
        case R.id.btnCapstone:
            showToast(this.btnCapstone.getText().toString());
            break;
    }
}

还有——只是想知道——你是不是在尝试获取按钮上的文本?或者来自其他地方的文本?

您正在声明onCreate中的所有按钮,因此您的类成员未初始化,因此它们为空,我猜是btnSpotifyApp第一次导致它

另外-尝试更正以下内容的拼写:

private Button btnSpotifyApp
与之相反:

btnSpotifyapp.setOnClickListener(this);
试试这个:

public class MainActivity extends ActionBarActivity implements View.OnClickListener{

private Button btnSpotifyApp, btnScoresApp, btnLibraryApp, btnBuildItBigger, btnXYZReader, btnCapstone;
private Toast mToast;

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

    this.btnSpotifyApp = (Button)findViewById(R.id.btnSpotifyApp);
    this.btnScoresApp = (Button)findViewById(R.id.btnScoresApp);
    this.btnLibraryApp = (Button)findViewById(R.id.btnLibraryApp);
    this.btnBuildItBigger = (Button)findViewById(R.id.btnBuildItBigger);
    this.btnXYZReader = (Button)findViewById(R.id.btnXYZReader);
    this.btnCapstone = (Button)findViewById(R.id.btnCapstone);

    btnSpotifyApp.setOnClickListener(this);
    btnScoresApp.setOnClickListener(this);
    btnLibraryApp.setOnClickListener(this);
    btnBuildItBigger.setOnClickListener(this);
    btnXYZReader.setOnClickListener(this);
    btnCapstone.setOnClickListener(this);
}

/**
 * Called when a view has been clicked.
 *
 * @param v The view that was clicked.
 */
@Override
public void onClick(View v) {
    switch (v.getId()) {
        //SignIn Button Clicked
        case R.id.btnSpotifyApp:
            showToast(this.btnSpotifyApp.getText().toString());
            break;
        case R.id.btnScoresApp:
            showToast(this.btnScoresApp.getText().toString());
            break;
        case R.id.btnLibraryApp:
            showToast(this.btnLibraryApp.getText().toString());
            break;
        case R.id.btnBuildItBigger:
            showToast(this.btnBuildItBigger.getText().toString());
            break;
        case R.id.btnXYZReader:
            showToast(this.btnXYZReader.getText().toString());
            break;
        case R.id.btnCapstone:
            showToast(this.btnCapstone.getText().toString());
            break;
    }
}

还有——只是想知道——你是不是在尝试获取按钮上的文本?或者来自其他地方的文本?

onCreate
中,它应该是
btnSpotifyapp=(按钮)findViewById(R.id.btnSpotifyapp)
按钮btnSpotifyapp=(按钮)findViewById(R.id.btnSpotifyapp),对于创建时的每个按钮,它应该是
btnSpotifyapp=(按钮)findViewById(R.id.btnSpotifyapp)
按钮btnSpotifyapp=(按钮)findViewById(R.id.btnSpotifyapp),此按钮用于每个按钮单击按钮中的文本以获取祝酒词。(在本例中)与代码中前面的“this.”有什么区别吗?在您的帖子中-是的,因为您有同名的成员和局部变量,在我写的文章中-否,因为成员是类范围中唯一的变量,为了清晰起见,我添加了它:)只是按钮中的文本。(在本例中)与代码中前面的“this.”有区别吗?在您的帖子中-是的,因为您有同名的成员和局部变量,在我写的文章中-否,因为成员是类范围中唯一的变量,为了清晰起见,我添加了它:)出于某种原因,我认为我必须添加另一个“按钮”,但你是对的,我没有使用我在类中声明的按钮。出于某种原因,我认为我必须添加另一个“按钮”,但你是对的,我没有使用我在类中声明的按钮。