I have a strange problem I don't understand...
I want to do a PopupWindow from an XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#404040"
android:padding="15px">
<TextView android:id="@+id/popup_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
andr开发者_StackOverflow中文版oid:textColor="#ffffff"
android:textSize="16dip"
android:gravity="center_horizontal" />
</LinearLayout>
and I just want to do a so simple thing: set the text in my TextView... Here is my code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//UI
ui = new RelativeLayout(this);
ui.setBackgroundColor(Color.LTGRAY);
setContentView(ui);
//PopUp
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
popUp = new PopupWindow(this);
popUp.setContentView(inflater.inflate(R.layout.popup_piece, ui, false));
//Boutton
bouton = new Button(this);
bouton.setText("POWPEUP");
ui.addView(bouton);
bouton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
popUp.showAtLocation(ui, Gravity.CENTER, 0, 0);
popUp.update(0, 0, 350, 400);
titrePopUp = (TextView)findViewById(R.id.popup_title); // THIS RETURNS NULL
Log.d("TextView", ""+titrePopUp);
//titrePopUp.setText("blop", TextView.BufferType.NORMAL); SO THIS DONT WORK
}
});
}
Seriously, I don't understand why it returns a NULL value.. Can anyone help me, please?
You are searching for the TextView in your main layout, not in the PopupWindow. You need to do this instead:
titrePopUp = (TextView) popUp.getContentView().findViewById(R.id.popup_titre);
In layout id is spelled as popup_title
, and in code it's spelled as popup_titre
. Maybe, this is the problem.
精彩评论