添加按钮布局编程 [英] Add button to a layout programmatically

查看:149
本文介绍了添加按钮布局编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法添加一个按钮,我在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()));

}

我想一个按钮添加到这个新的屏幕,这样它会显示在图形下方。 我试图创建一个的LinearLayout 视图,然后创建一个按钮,并把它添加到这个观点,但我只是得到 NullPointerException异常秒。

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 NullPointerExceptions..

任何帮助将是AP preciated。谢谢

Any help would be appreciated. Thanks

编辑#1

下面是我使用创建一个 NullPointerException异常和强制关闭已经试过:

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);

}

和这里的logcat的错误:

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)

孤单abviously多线做在logcat的这个错误,不知道你想要呢?

theres abviously more lines to do with this error in logcat, not sure if you want it?

编辑#2

所以我尝试bhups方式:

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);           
}

此方法产生相同的logcat错误如上, NullPointerException异常,表明它是是与线没有。 27这是 layout.addView 线code。有任何想法吗?再次感谢

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

推荐答案

这行:

layout = (LinearLayout) findViewById(R.id.statsviewlayout);

会在你目前的内容查看的statsviewlayout的ID。现在你已经设置在这里:

Looks for the "statsviewlayout" id in your current 'contentview'. Now you've set that here:

setContentView(new GraphTemperature(getApplicationContext()));

和我猜测,新的graphTemperature不设置与该ID事情。

And i'm guessing that new "graphTemperature" does not set anything with that id.

这是一个常见的​​错误以为你可以找到findViewById任何看法。你只能找到一个观点,即是在XML(或code委任,并给出一个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.

另外: 现在,如果你想从一个XML这种看法,你应该使用吹气:

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);

假设你有你的LinearLayout在一个名为yourXMLYouWantToLoad.xml文件

assuming that you have your linearlayout in a file called "yourXMLYouWantToLoad.xml"

这篇关于添加按钮布局编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆