Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7639671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:28:42+00:00 2026-05-31T08:28:42+00:00

I’m new to android developing and I’m trying to open a popup window from

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T08:28:44+00:00Added an answer on May 31, 2026 at 8:28 am

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I am currently running into a problem where an element is coming back from
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.