0

我只做了一个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;
        };

    }
}

也许我只是输入了错误的东西,但我在我的源代码中找不到超过两天的错误。感谢您的帮助。

4

3 回答 3

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 命名约定建议使用小写字母作为变量名的首字母。

于 2013-01-31T16:52:29.903 回答
1

在 swich case 后删除 smi-colon :

 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);
    }
于 2013-01-31T16:54:20.653 回答
0
public void initializeVars(){
    Button BUeasycounter= (Button) findViewById(R.id.BUeasycounter);
    Button BUrps = (Button) findViewById(R.id.BUrps);
}

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

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

从上面的方法中,它将实例变量中的引用分配给它的范围,直到这个类存在。最后从 switch 块的末尾删除分号。

于 2013-01-31T16:59:49.157 回答