public class Offer_Popup extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.offer_popup);
//newly added code, the window popup
LayoutInflater inflater = (LayoutInflater)
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
PopupWindow pw = new PopupWindow(
inflater.inflate(R.layout.popup_example, null, false),
100,
100,
true);
pw.showAtLocation(this.findViewById(R.id.relativeLayout_popup), Gravity.CENTER, 0, 0);}
/////////////////////
popup_example.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="Test Pop-Up"
/>
</LinearLayout>
DDMS shows the last line showAtLocation is the error.
It says Unable to add window–token null is invalid. android.view.WindowManagerBadToken.
Before adding the popup, the program runs fine. But after the popup code, it crashed. what’s the bad token it’s talking about?
A Popup Window requires an initial Window to be able to pop up from. One would think that an Activity automatically has a Window, but this is not the case right from the start. A given Activity does not have an active Window until
onAttachedToWindow(). At this time, you may add the PopupWindow to the Window becaue there is a Window present.If you move the code to the onAttachedToWindow(), it should work as intended or at least give you a different error entirely.
Hope this helps,
FuzzicalLogic