Reputation: 1099
I have got this class below and whenever I run this I get a runtime exception which I believe is caused by a Null Pointer Exception. I have included my Log Cat below.
Java File ;
public class Example extends Activity{
View v;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.example_layout);
v.findViewById(R.id.eamplebutton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("Testing", "sd");
}
});
}
@Override
public void onBackPressed() {
super.onBackPressed();
Intent i=new Intent(Example.this,MainActivity.class);
startActivity(i);
finish();
}
}
Upvotes: 1
Views: 124
Reputation: 157447
remove v
, from v.findViewById
findViewById(R.id.eamplebutton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("Testing", "sd");
}
});
Activity's findViewById
looks for the id
, in the Activity's view hierarchy, the one build starting from what you pass as parameter to setContentView
Upvotes: 1