Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android编程设置按钮文本_Android_Button - Fatal编程技术网

Android编程设置按钮文本

Android编程设置按钮文本,android,button,Android,Button,有人知道如何通过编程设置按钮的文本吗 问题是,我不是从主布局(setContentView)调用它,而是在异步任务后膨胀的视图中调用它 这是我尝试过的,但这在第2行给出了一个空指针异常 Button mButton=(Button)findViewById(R.id.contact); mButton.setText("number"); 这是我的布局,我在这里调用按钮 <Button android:id="@+id/contact"

有人知道如何通过编程设置按钮的文本吗

问题是,我不是从主布局(setContentView)调用它,而是在异步任务后膨胀的视图中调用它 这是我尝试过的,但这在第2行给出了一个空指针异常

Button mButton=(Button)findViewById(R.id.contact);
        mButton.setText("number");
这是我的布局,我在这里调用按钮

   <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"/>

您的
mButton
为空。因此NPE。您是否在
setContentView

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

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

}
将代码更改为:

@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");
编辑: 您的\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>

检查R.java文件导入语句

您确定已将其导入到您使用的项目中吗。。
只需格式化布局(.xml)文件并保存它,然后再次键入相同的语句,然后使用视图的对象对其进行初始化:

Button mButton = (Button)your_view_object.findViewById(R.id.contact);
mButton.setText("number");
当您试图识别活动布局以外的视图时,必须像这样传递该视图的引用。如果没有,Android将继续从您在
setContentView()
中提供的布局中查找此元素

例如,认为你夸大了这样的观点:

View View = mInflater.inflate(R.layout.gridelement, null);
然后将此视图的对象用于膨胀布局中的按钮:

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

您能在这里发布完整的onCreate方法吗?请提供完整的java代码文件。它会给出NPE,因为mButton在第一行为null。这是因为findViewById没有找到您提供给它的R.id.contact。问题不会出现在java文件中,但在XmlView中TextView是否正常工作?如果是,,您的文本视图和代码/xml中的按钮有什么区别?问题是它没有从内容视图加载,它是一个膨胀视图中的一个按钮,我在Asynctask@LukeBatley:由于你的问题得到了正确的答案。如果问题没有解决,请发布更多的代码和完整的解释problem@LukeBatley :使用
LayoutInflater充气器=(LayoutInflater)上下文.getSystemService(布局充气器服务);View ClubInfo=充气机。充气(R.layout.clubinfocell,null)
而不是
ClubInfo=LayoutInflater.from(getBaseContext()).flate(R.layout.clubinfocell,null)我已经编辑了我的代码来显示我的视图,我仍然得到了围绕这个的空指针异常,如果(mButton!=null){mButton.setText(电话);}
  Button mButton = (Button)View.findViewById(R.id.contact);