我正在开发Android应用。出于某种原因,每当我向onCreate添加任何代码时,应用程序都会在启动时崩溃。
以下是Eclipse生成的内容:
@Override
protected void onCreate(Bundle exampleApp) {
super.onCreate(exampleApp);
setContentView(R.layout.activity_main);
if (exampleApp == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
随着它的发布就好了,但显然它并没有真正做任何事情。只要我添加代码来使按钮做某事,就像这样:
@Override
protected void onCreate(Bundle exampleApp) {
super.onCreate(exampleApp);
setContentView(R.layout.activity_main);
if (exampleApp == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
final Button doSomething = (Button) findViewById(R.id.doSomethingButton);
doSomething.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Insert code to execute on button press here
}
});
}
应用程序在启动时崩溃。我不知道发生了什么,因为根据我能找到的所有文档,我正确地做到了,而且我实际上已经在这种方式下制作了应用程序,所以到底出了什么问题,我该如何解决呢? / p>