Reputation: 172
Button start_game;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
start_game = (Button) findViewById(R.id.start_game);
start_game.setOnClickListener(this);
setContentView(R.layout.welcome);
}
I don't know why, but if I remove the line setOnClickListener
my app starts (of course my button doesn't do anything then).
Logcat gives me this:
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.test.testabc/de.test.testabc.Welcome}: java.lang.NullPointerException
Upvotes: 0
Views: 370
Reputation: 93842
You have to inflate your layout before getting the UI elements, otherwise findViewById
returns null
and hence you got the NullPointerException
at the line start_game.setOnClickListener(this);
.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome); //layout inflated now it's ok to get your button
start_game = (Button) findViewById(R.id.start_game);
start_game.setOnClickListener(this);
}
Upvotes: 8