Reputation: 11
Can you please tell that this same program runs on one one machine and will not execute on another? It states null pointer exception on the other. When I click on CheckBox, it says unfortunately your activity has stopped.
Below is the code :-
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));
}
}
Upvotes: 0
Views: 53
Reputation: 24820
I dont see textView t1 being initialized, maybe that is the reason you are getting nullpointer exception
Instead of
t2=(TextView)findViewById(R.id.textView1);
use
t1=(TextView)findViewById(R.id.textView1);
Upvotes: 2