dark_illusion_909099
dark_illusion_909099

Reputation: 1099

Android Project - RuntimeException , caused by Null Pointer Exception

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();
}
}

LogCat

Upvotes: 1

Views: 124

Answers (1)

Blackbelt
Blackbelt

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

Related Questions