强制关闭由onClickListener引起

时间:2013-01-31 16:51:09

标签: java android android-intent android-activity onclicklistener

我只打了一个activity,打开另一个activity。一切正常,直到我设置onClickListener。现在我的应用程序在启动时关闭。没有这两行应用程序正确启动:

BUeasycounter.setOnClickListener(this);
BUrps.setOnClickListener(this);

以下是我的完整资料来源:

package com.dano.learning;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Menu extends Activity implements View.OnClickListener{

    Button BUeasycounter;
    Button BUrps;
    @Override
    protected void onCreate(Bundle MyBundle) {
        super.onCreate(MyBundle);
        setContentView(R.layout.activity_main);
        initializeVars();
// next 2 lines cause the problem
        BUeasycounter.setOnClickListener(this);
        BUrps.setOnClickListener(this);
    }
    public void initializeVars(){
        Button BUeasycounter= (Button) findViewById(R.id.BUeasycounter);
        Button BUrps = (Button) findViewById(R.id.BUrps);
    }

    public void onClick(View v) {
        switch (v.getId()){
            case R.id.BUeasycounter:
                Intent openEasyCounter = new Intent("com.dano.learning.EASYCOUNTER");
                startActivity(openEasyCounter);
                break;
            case R.id.BUrps:
                Intent openRPS = new Intent("com.dano.learning.EASYCOUNTER");
                startActivity(openRPS);
                break;
        };

    }
}

也许我只是输入了错误的内容,但我在源代码中发现错误超过两天。谢谢你的帮助。

3 个答案:

答案 0 :(得分:2)

没有问题的异常堆栈,但根据代码,我看到一个问题是:

    Button BUeasycounter;
    Button BUrps;

两者都指向null,当您执行

时可能会抛出NullPointerException
    BUeasycounter.setOnClickListener(this);
    BUrps.setOnClickListener(this);

initializeVars方法中删除新变量。

示例:

public void initializeVars(){
          BUeasycounter= (Button) findViewById(R.id.BUeasycounter);
          BUrps = (Button) findViewById(R.id.BUrps);
    }

注意:Java命名约定建议使用小写字母作为变量名的第一个字母。

答案 1 :(得分:1)

切换案例后删除分号:

 public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()){
            case R.id.BUeasycounter:
                Intent openEasyCounter = new Intent("com.dano.learning.EASYCOUNTER");
                startActivity(openEasyCounter);
                break;
            case R.id.BUrps:
                Intent openRPS = new Intent("com.dano.learning.EASYCOUNTER");
                startActivity(openRPS);
                break;
        } //>>>> from here remove semi-colon 

并将initializeVars方法更改为:

  public void initializeVars(){
          BUeasycounter= (Button) findViewById(R.id.BUeasycounter);
          BUrps = (Button) findViewById(R.id.BUrps);
    }

答案 2 :(得分:0)

public void initializeVars(){
    Button BUeasycounter= (Button) findViewById(R.id.BUeasycounter);
    Button BUrps = (Button) findViewById(R.id.BUrps);
}

您在此方法中定义了两个按钮,但这些是局部变量,其范围位于此块之间。 你不能从这里访问。您已初始化局部变量但实例按钮变量 仍然有null。要从NPE获得解决方案,你应该写为

public void initializeVars(){
    BUeasycounter= (Button) findViewById(R.id.BUeasycounter);
    BUrps = (Button) findViewById(R.id.BUrps);
}

从上面的方法中,它将实例变量的引用分配给它的范围,直到该类存在。 并最后从开关块的末端删除半冒号。