Android 以编程方式设置按钮文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11154898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:11:07  来源:igfitidea点击:

Android Programmatically setting button text

androidbutton

提问by Luke Batley

Does anybody know how to programmatically set the text of a button?

有人知道如何以编程方式设置按钮的文本吗?

thing is i'm not calling this from the main layout(setContentView) i'm calling it in a view thats inflated after an asynctask heres what i have tried but this is giving a null pointer exception on the 2nd line

事情是我不是从主布局(setContentView)调用它我是在异步任务之后膨胀的视图中调用它这是我尝试过的但是这在第二行给出了空指针异常

Button mButton=(Button)findViewById(R.id.contact);
        mButton.setText("number");

heres my layout where i am calling the button

这是我调用按钮的布局

   <Button
       android:id="@+id/contact"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@+id/address"
       android:layout_toLeftOf="@+id/badge"
       android:background="@drawable/ic_btn_call"
       android:textSize="10dp"
       android:textStyle="bold"
       android:textColor="@color/white"/>

here my view i'm inflating

这是我的观点,我在膨胀

ClubInfo = LayoutInflater.from(getBaseContext()).inflate(R.layout.clubinfocell,
                null);

        TextView TeamNameText = (TextView) ClubInfo.findViewById(R.id.TeamName);
        TeamNameText.setText(teamName);

        TextView AddressText = (TextView) ClubInfo.findViewById(R.id.address);
        AddressText.setText(address1);



        Button mButton=(Button)ClubInfo.findViewById(R.id.contact);
        mButton.setText(telephone);

回答by Andro Selva

Then use your view's object to initialize it:

然后使用您的视图对象对其进行初始化:

Button mButton = (Button)your_view_object.findViewById(R.id.contact);
mButton.setText("number");

When you try to identify a view other than your Activity's layout, you have to pass the reference of that view like this. If not Android will keep looking for this element from the layout which you provided in the setContentView().

当您尝试识别 Activity 布局以外的视图时,您必须像这样传递该视图的引用。如果不是,Android 将继续从您在setContentView().

For example, consider you have inflated a view like this:

例如,假设您已经夸大了这样的视图:

View View = mInflater.inflate(R.layout.gridelement, null);

Then use this View's object for the Button present in that inflated layout:

然后将此视图的对象用于该膨胀布局中存在的按钮:

  Button mButton = (Button)View.findViewById(R.id.contact);

回答by ρяσ?ρ?я K

change your code as:

将您的代码更改为:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);//set layout here in which u have add contact in xml
        Button mButton=(Button)findViewById(R.id.contact);
        mButton.setText("number");

EDIT:Your \res\layout\main.xml look like as:

编辑:你的 \res\layout\main.xml 看起来像:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
<Button
       android:id="@+id/contact"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@+id/address"
       android:layout_toLeftOf="@+id/badge"
       android:background="@drawable/ic_btn_call"
       android:textSize="10dp"
       android:textStyle="bold"
       android:textColor="@color/white"/>
</LinearLayout>

回答by Samir Mangroliya

your mButtonis null.so NPE.are you refrenced xml resources after setContentView

mButton是 null.so NPE.are you refrened xml resources aftersetContentView

onCreate(){
...
setContentView(R.layout.yourlayout);

Button mButton=(Button)findViewById(R.id.contact);
mButton.setText("number");

}

回答by Freni

check R.java files import statement

检查 R.java 文件导入语句

are you sure that you have import it of the project you use .. and just format your layout (.xml ) file save it and again type the same statement

你确定你已经导入了你使用的项目吗 .. 并且只是格式化你的布局 (.xml ) 文件保存它并再次输入相同的语句