1

以下のコードを実行すると、次のようなメッセージが表示されます。

 the application stopped unexcepctedlly pleas try again

誰でもこの問題に対処するのを手伝うことができます。

public class TheNewActivity extends Activity {
    /** Called when the activity is first created. */
    int counter;
    Button sub,add;
    TextView display;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        add=(Button) findViewById(R.id.button1);
        display=(TextView) findViewById(R.id.dis);

     add.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            counter++;
            display.setText("your total is"+counter);
        }
    }); 
     sub.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            counter--;
            display.setText("your total is"+counter);
        }
    });
    }
}
4

4 に答える 4

1

あなたは「サブ」をインスタンス化していません.......そして、あなたはそれを実装していますonClickListner()

于 2012-06-18T11:50:34.140 に答える
1

onClickListener を設定する前に、これを追加する必要があります。

sub=(Button) findViewById(R.id.urbtnname);
于 2012-06-18T11:50:42.427 に答える
1

Button を初期化していませんsub。コードに追加します。

sub=(Button) findViewById(R.id.button2); // if button2 is the id for sub in xml file.
于 2012-06-18T11:50:47.590 に答える
1

ここには、このようなサブボタン参照はありません add=(Button) findViewById(R.id.button1);

ありがとう

于 2012-06-18T11:51:32.270 に答える