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);
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
Then use your view's object to initialize it:
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:
Then use this View's object for the Button present in that inflated layout:
your
mButton
is null.so NPE.are you refrenced xml resources aftersetContentView
change your code as:
EDIT: Your \res\layout\main.xml look like as: