0

この同じプログラムは、あるマシンでは実行され、別のマシンでは実行されないことを教えてください。もう一方にはヌルポインタ例外が記載されています。CheckBox をクリックすると、残念ながらアクティビティが停止したと表示されます。

以下はコードです:-

package com.example.gtbactivity;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity implements OnCheckedChangeListener, OnClickListener {

    CheckBox cb1,cb2;
    TextView t1,t2,t3;
    Button b;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cb1=(CheckBox)findViewById(R.id.checkBox1);
        cb2=(CheckBox)findViewById(R.id.checkBox2);
        b=(Button)findViewById(R.id.button1);   
        t1=(TextView)findViewById(R.id.textView1);
        t2=(TextView)findViewById(R.id.textView2);
        t3=(TextView)findViewById(R.id.textView3);


        b.setOnClickListener(this);
                cb1.setOnCheckedChangeListener(this);
                cb2.setOnCheckedChangeListener(this);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }



    @Override
    public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
         if(cb1.isChecked())
         { t1.setText("10");}
         else
         {t1.setText("0");}


         if(cb2.isChecked())
         { t2.setText("15");}
         else
         {t2.setText("0");}


    }
    @Override
    public void onClick(View arg0) {
        int total,a,b;
        a=Integer.parseInt(t1.getText().toString());
        b=Integer.parseInt(t2.getText().toString());

        total=a+b;
        t3.setText(String.valueOf(total));

    }

}
4

1 に答える 1

2

初期化されている textView t1 が表示されないため、nullpointer 例外が発生している可能性があります

それ以外の

    t2=(TextView)findViewById(R.id.textView1);

使用する

    t1=(TextView)findViewById(R.id.textView1);
于 2012-12-17T14:12:43.063 に答える