我给你的代码:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
RelativeLayout layout = (RelativeLayout)findViewById(R.id.disposizione);
Button pulsante = new Button(this);
pulsante.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
pulsante.setText("Click me");
pulsante.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Thug life bitch!", Toast.LENGTH_LONG).show();
}
});
layout.addView(pulsante);
setContentView(R.layout.main);
}
我试图将所有行从RelativeLayout删除到layout.addView,所以它又可以工作了!
这段代码有什么问题?我必须说我没有任何构建错误或警告。
提前致谢。
最佳答案
在setContentView()
之后立即写super.onCreate()
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
关于android - 为什么这个Android应用程序立即崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39495198/