gpt4 book ai didi

java - 在 Activity 中获取按钮文本时出现 NullPointerException

转载 作者:行者123 更新时间:2023-11-29 07:40:43 25 4
gpt4 key购买 nike

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

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

关于java - 在 Activity 中获取按钮文本时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30547104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com