你能告诉我这个相同的程序在一台机器上运行并且不会在另一台机器上执行吗?它在另一个上声明空指针异常。当我单击 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));
}
}