在Activity中获取Button Text的NullPointerException

时间:2015-05-30 14:02:39

标签: java android

我试图编写一个简单的应用程序,在单击布局中的按钮时输出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)

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:2)

你正在重新声明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;
    }
}

也 - 只是想知道 - 你是否想要获取按钮上的文字?或来自其他地方的文字?

答案 2 :(得分:1)

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

你应该这样做:

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