以编程方式将按钮添加到布局 [英] Add button to a layout programmatically
问题描述
我无法向我用 XML 创建的布局添加按钮.这是我想要实现的目标:
//某个类别的 {开始活动(新意图(StatisticsScreen.this,ScreenTemperature.class));}//////屏幕温度类@覆盖public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//这是我调用另一个类的地方//显示一个漂亮的图形setContentView(new GraphTemperature(getApplicationContext()));}
我想在这个新屏幕上添加一个 Button
,这样它就会出现在图表下方.我尝试创建一个 LinearLayout
视图,然后创建一个 Button
并将其添加到此视图中,但我只得到 NullPointerException
s..>
任何帮助将不胜感激.谢谢
编辑#1
这是我尝试使用的创建 NullPointerException
和强制关闭"的方法:
按钮购买按钮;线性布局布局;@覆盖public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(new GraphTemperature(getApplicationContext()));layout = (LinearLayout) findViewById(R.id.statsviewlayout);按钮buyButton = new Button(this);buyButton.setText(R.string.button_back);buyButton.setLayoutParams(新的LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));layout.addView(buyButton);}
这里是 logcat 错误:
ERROR/AndroidRuntime(293): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.weatherapp/com.weatherapp.ScreenTemperature}: java.lang.NullPointerException错误/AndroidRuntime(293): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)错误/AndroidRuntime(293): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)错误/AndroidRuntime(293): 在 android.app.ActivityThread.access$2300(ActivityThread.java:125)错误/AndroidRuntime(293): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
显然在 logcat 中有更多行与此错误有关,不知道您是否需要?
编辑#2
所以我尝试了 bhups 方法:
@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);GraphTemperature GT = new GraphTemperature(getApplicationContext());layout = (LinearLayout) findViewById(R.id.statsviewlayout);按钮buyButton = new Button(this);buyButton.setText(R.string.button_back);buyButton.setLayoutParams(新的LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));layout.addView(GT);//第 27 行layout.addView(buyButton);设置内容视图(布局);}
此方法产生了与上面相同的 logcat 错误,NullPointerException
,表明它与第 1 行有关.27 这是 layout.addView
代码行.有任何想法吗?再次感谢
这一行:
layout = (LinearLayout) findViewById(R.id.statsviewlayout);
在您当前的内容视图"中查找statsviewlayout"ID.现在你已经在这里设置了:
setContentView(new GraphTemperature(getApplicationContext()));
而且我猜新的graphTemperature"没有设置任何具有该 ID 的内容.
认为您可以使用 findViewById 找到任何视图是一个常见的错误.您只能找到XML 中的视图(或由代码指定并给定id).
空指针会被抛出,因为找不到你要找的布局,所以
layout.addView(buyButton);
抛出那个异常.
补充:现在,如果您想从 XML 获取该视图,则应使用充气器:
layout = (LinearLayout) View.inflate(this, R.layout.yourXMLYouWantToLoad, null);
假设您在名为yourXMLYouWantToLoad.xml"的文件中有您的线性布局
I'm having trouble adding a button to a layout that I've created in XML. Here's what I want to achieve:
//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()));
}
I want to add a Button
to this new screen so that it'll appear below the graph.
I've tried creating a LinearLayout
view, then create a Button
and add it to this view but I just get NullPointerException
s..
Any help would be appreciated. Thanks
EDIT#1
Here's what I've tried using that created a NullPointerException
and 'force close':
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);
}
And here's the logcat error:
ERROR/AndroidRuntime(293): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.weatherapp/com.weatherapp.ScreenTemperature}: java.lang.NullPointerException
ERROR/AndroidRuntime(293): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
ERROR/AndroidRuntime(293): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
ERROR/AndroidRuntime(293): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
ERROR/AndroidRuntime(293): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
theres abviously more lines to do with this error in logcat, not sure if you want it?
EDIT#2
So i tried bhups method:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GraphTemperature GT = 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(GT); // line 27
layout.addView(buyButton);
setContentView(layout);
}
This method produced the same logcat error as above, NullPointerException
, indicating it was something to do with line no. 27 which is the layout.addView
line of code. Any ideas? Thanks again
This line:
layout = (LinearLayout) findViewById(R.id.statsviewlayout);
Looks for the "statsviewlayout" id in your current 'contentview'. Now you've set that here:
setContentView(new GraphTemperature(getApplicationContext()));
And i'm guessing that new "graphTemperature" does not set anything with that id.
It's a common mistake to think you can just find any view with findViewById. You can only find a view that is in the XML (or appointed by code and given an id).
The nullpointer will be thrown because the layout you're looking for isn't found, so
layout.addView(buyButton);
Throws that exception.
addition: Now if you want to get that view from an XML, you should use an inflater:
layout = (LinearLayout) View.inflate(this, R.layout.yourXMLYouWantToLoad, null);
assuming that you have your linearlayout in a file called "yourXMLYouWantToLoad.xml"
这篇关于以编程方式将按钮添加到布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!