0

私はactivity別のものを開くだけを作りましたactivity。onClickListener をセットアップするまで、すべてが正しく機能していました。これで、アプリが起動すると強制終了します。これらの 2 行がないと、アプリケーションは正しく起動します。

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

    }
}

多分私は何か間違って入力しただけかもしれませんが、2日以上ソースに間違いを見つけることができません. ご協力ありがとうございました。

4

3 に答える 3

2

問題の例外スタックはありませんが、コードに基づくと、次のような問題が 1 つあります。

    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

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

このメソッドで 2 つのボタンを定義しますが、これらはスコープがこのブロックの間にあるローカル変数です。ここから外部にアクセスすることはできません。ローカル変数を初期化しましたが、インスタンス ボタン変数にはまだ null があります。NPE からソリューションを取得するには、次のように記述する必要があります。

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

上記のメソッドから、このクラスが存在するまで、インスタンス変数の参照をそのスコープに割り当てます。最後に、switch ブロックの末尾からセミコロンを削除します。

于 2013-01-31T16:59:49.157 に答える