19

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

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);
6
  • can you post your full onCreate method here? Commented Jun 22, 2012 at 11:01
  • 1
    please provide your full java code file. Commented Jun 22, 2012 at 11:01
  • 1
    It gives NPE because mButton is null on first line. And that's is because findViewById didn't find a R.id.contact you provided to it. Commented Jun 22, 2012 at 11:02
  • Problem will not be in the java file, but in the xml Commented Jun 22, 2012 at 11:59
  • Does textViews work correctly? If so, what is the difference between your TextViews and your Button in code/xml? Commented Jun 22, 2012 at 12:00

4 Answers 4

33

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().

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);
Sign up to request clarification or add additional context in comments.

2 Comments

i have edited my code to show my view i'm still getting null pointer exception
Try surrounding this, with null check, if(mButton!=null) { mButton.setText(telephone);}
2

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:

<?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>

3 Comments

problem is its not been loaded from the content view its a button within a inflated view that i have inflated after an Asynctask
@LukeBatley : as your question u are getting all right answers.if problem is not solved then post more code and full explain of problem
@LukeBatley : use LayoutInflater inflater = (LayoutInflater)context.getSystemService(LAYOUT_INFLATER_SERVICE); View ClubInfo=inflater.inflate(R.layout.clubinfocell, null); instead of ClubInfo = LayoutInflater.from(getBaseContext()).inflate(R.layout.clubinfocell, null);
1

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

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

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

}

Comments

0

check R.java files import statement

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.