I’m new to android developing and I’m trying to open a popup window from my main class, which is created on another class.
The problem is that I’m getting the following error when I’m trying to call the popup via a button click on main class:
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
If I move the code for popup to the button’ s case, I can open by click on it, but I cannot use any of methods are on the other class(at least I don’t know how).
I’ve search a lot and found some similar threads, but everything I tried failed, and the error didn’t change.
So, I prefer to have all code for popup in the second class and just call it from main class, but doesn’t bother me if I can have access to the methods of the second class.
Here is the code of popup window:
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
LayoutInflater inflater = (LayoutInflater) getSystemService (Context.LAYOUT_INFLATER_SERVICE);
//Inflate the view from a predefined XML layout
View layout = inflater.inflate(R.layout.keyboard,
(ViewGroup) findViewById(R.id.root));
// create a 300px width and 470px height PopupWindow
final PopupWindow pw = new PopupWindow(layout, 590, 400, true);
pw.setAnimationStyle(android.R.style.Animation_Dialog);
pw.setBackgroundDrawable(new BitmapDrawable());
pw.setOutsideTouchable(true);
// display the popup in the center
pw.showAtLocation(layout, Gravity.TOP, 0, 50);
And the main class button’s code:
case R.id.bkeyb:
startActivity(new Intent(this, keyboard.class));
break;
I desided to include all code for popup to the main activity and delete the second class.
So I made a method popup, which include the above code for popup window under the onCreate of main class. I tested it with the button which I want to open the popup and worked.
Then I made an Initialise method for all buttons of popup window, in which use findViewById for the resources and onClickListeners definition.
The code now for popup became:
public void Popup(){
LayoutInflater inflater = (LayoutInflater) getSystemService (Context.LAYOUT_INFLATER_SERVICE);
//Inflate the view from a predefined XML layout
View layout = inflater.inflate(R.layout.keyboard,
(ViewGroup) findViewById(R.id.root));
initialize();
// create a 300px width and 470px height PopupWindow
final PopupWindow pw = new PopupWindow(layout, 590, 400, true);
pw.setAnimationStyle(android.R.style.Animation_Dialog);
pw.setBackgroundDrawable(new BitmapDrawable());
pw.setOutsideTouchable(true);
// display the popup in the center
pw.showAtLocation(layout, Gravity.TOP, 0, 50);
}
And the onClickListener code for button which launch popup window:
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.bkeyb:
Popup();
switch(v.getId()) {
case R.id.balpha:
tvcheck.setText("Α");
Log.e(null, "you pressed a");
break;
case R.id.bbeta:
tvcheck.setText("Β");
break;
}
break;
I just added 2 buttons for testing.
Now when press the button I get a crash with a NullPointerException.
So again something is wrong.
Android Framework defined three kinds of window types.Application window,Sub window,System window. Popup window belongs to Sub window and Sub Window must have a parent window.So you cannot set the popup window to be a activity’s main window.poor english,hope you can understand what i mean!