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);
}
Run Code Online (Sandbox Code Playgroud)
我不知道为什么,但如果我删除setOnClickListener我的应用程序启动的行(当然我的按钮不会做任何事情).Logcat给了我这个:
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.test.testabc/de.test.testabc.Welcome}: java.lang.NullPointerException
你必须在获取UI元素之前给你的布局充气,否则findViewById返回null,因此你得到NullPointerException了线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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
359 次 |
| 最近记录: |