2012-06-22 55 views
10

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

事情是我没有从主布局(setContentView)调用这个我打电话给一个视图,这是一个asynctask后膨胀 继承人我试过,但这是给第二行空指针异常

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

这里我认为我正在充气

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

你可以发表你的全onCreate方法这里? – Cata

+1

请提供完整的java代码文件。 –

+1

它给NPE,因为mButton在第一行为空。那是因为findViewById没有找到你提供给它的R.id.contact。 –

回答

20

然后使用您的视图的对象初始化它:

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

我编辑了我的代码来显示我的视图我仍然收到空指针异常 –

+0

尝试围绕这个,用null检查,如果(mButton!= null){mButton.setText(telephone);} –

1

mButton是null.so NPE.are你refrenced XML资源后setContentView

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

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

} 
1

改变你的代码为:

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

编辑: 你\水库\布局\为主。 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> 
+0

问题是它没有从内容视图加载它的一个按钮内充气的观点,我已经充气后Asynctask –

+0

@LukeBatley:作为你的问题你得到所有正确的answers.if问题没有解决,然后发布更多的代码和完整问题解释 –

+0

@LukeBatley:使用'LayoutInflater inflater =(LayoutInflater)context.getSystemService(LAYOUT_INFLATER_SERVICE); View ClubInfo = inflater.inflate(R.layout.clubinfocell,null); '而不是'ClubInfo = LayoutInflater。from(getBaseContext())。inflate(R.layout.clubinfocell, null); ' –

0

检查R.java文件我M端口声明

你确定你有导入它使用项目.. 的,只是格式化您的布局(.XML)文件保存并重新键入相同的语句

相关问题