我在向XML创建的布局中添加按钮时遇到了问题.这就是我想要实现的目标:
//some class
else {
startActivity(new Intent(StatisticsScreen.this, ScreenTemperature.class));
}
////
//ScreenTemperatureClass
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//this is where I call another class that
//displays a nice graph
setContentView(new GraphTemperature(getApplicationContext()));
}
Run Code Online (Sandbox Code Playgroud)
我想在Button这个新屏幕上添加一个,以便它显示在图表下方.我已经尝试创建一个LinearLayout视图,然后创建一个Button并将其添加到此视图但我只是得到了NullPointerException...
任何帮助,将不胜感激.谢谢
编辑#1
这是我尝试使用的创建了一个NullPointerException'强制关闭':
Button buybutton;
LinearLayout layout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new GraphTemperature(getApplicationContext()));
layout = (LinearLayout) findViewById(R.id.statsviewlayout);
Button buyButton = new Button(this);
buyButton.setText(R.string.button_back);
buyButton.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
layout.addView(buyButton);
}
Run Code Online (Sandbox Code Playgroud)
这是logcat错误:
ERROR/AndroidRuntime(293): java.lang.RuntimeException: Unable …Run Code Online (Sandbox Code Playgroud)