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 3392862
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:58:56+00:00 2026-05-18T03:58:56+00:00

Within a public class that doesn’t extend any other class I’m doing some evaluation

  • 0

Within a public class that doesn’t extend any other class I’m doing some evaluation stuff and I would like to return a Dialog if something needs to be communicated to the user.

I’m using an AsyncTask to run a method of an instance of this class from the main UI:

private OnlineActivities onlineActivities = new OnlineActivities();

new DoOnlineActivity().execute(getApplicationContext());

private class DoOnlineStuff extends AsyncTask<Context, Integer, Dialog> {
    @Override
    protected Dialog doInBackground(Context... params) {
        return onlineActivities.start(params[0]);
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        Log.v(RuntimeVars.getMyName(), "AsyncOnlineTask in progess");
    }

    @Override
    protected void onPostExecute(Dialog result) {
        if (result != null)
            result.show();
    }
}

Now, the public Dialog start() method of OnlineActivities.java creates a dialog using the Context that was assigned via it’s parameter.

When the Dialog is returned to onPostExecute I receive the following exception

11-14 23:53:43.303: ERROR/AndroidRuntime(1320): FATAL EXCEPTION: main
11-14 23:53:43.303: ERROR/AndroidRuntime(1320): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
11-14 23:53:43.303: ERROR/AndroidRuntime(1320):     at android.view.ViewRoot.setView(ViewRoot.java:509)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320):     at android.app.Dialog.show(Dialog.java:241)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320):     at info.myProject.messaging.ConversationList$DoOnlineActivity.onPostExecute(ConversationList.java:245)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320):     at info.myProject.messaging.ConversationList$DoOnlineActivity.onPostExecute(ConversationList.java:1)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320):     at android.os.AsyncTask.finish(AsyncTask.java:417)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320):     at android.os.AsyncTask.access$300(AsyncTask.java:127)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320):     at android.os.Looper.loop(Looper.java:123)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320):     at android.app.ActivityThread.main(ActivityThread.java:4627)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320):     at java.lang.reflect.Method.invokeNative(Native Method)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320):     at java.lang.reflect.Method.invoke(Method.java:521)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320):     at dalvik.system.NativeStart.main(Native Method)

I suppose I cannot use the context like this. But what would be the alternative? How can I outsource some basic stuff I always need to do, even some general Dialogs?

Do I need to use custom intents instead?

  • 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-18T03:58:57+00:00Added an answer on May 18, 2026 at 3:58 am

    I got a part of it to work. The problem was the usage of getApplicationContext(). The Android SDK states

    Return the context of the single, global Application object of the current process.

    I suppose this context differs from the context of the actual activity.

    So, the Dialog gets displayed when I use this instead of getApplicationContext():

    private OnlineActivities onlineActivities = new OnlineActivities();
    
    new DoOnlineActivity(this).execute();
    
    private class DoOnlineStuff extends AsyncTask<Void, Integer, Dialog> {
        Context ctx;
        Dialog dialog;
    
        public DoOnlineActivity(Context ctx){
            this.ctx = ctx;
        }
    
        @Override
        protected Dialog doInBackground(Void... params) {
            return onlineActivities.start(ctx);
        }
    
        @Override
        protected void onProgressUpdate(Integer... progress) {
            Log.v(RuntimeVars.getMyName(), "AsyncOnlineTask in progess");
        }
    
        @Override
        protected void onPostExecute(Dialog result) {
            if (result != null) {
                dialog = result;
                dialog.show();
            }
        }
    }
    

    BUT: Now the button I created within the Dialog won’t do anything. I guess the OnClickListener() within my Dialog isn’t responsive in the context of my main ui:

    final View layout = View.inflate(ctx, R.layout.dialog_phonenumber_request, null);
    ((TextView) layout.findViewById(R.id.label)).setText(ctx.getString(R.string.dialog_insert_self_phonenumber_request_text1));
    
    final EditText savedPhoneNumber = ((EditText) layout.findViewById(R.id.phonenum));
    AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
    builder.setIcon(0);
    builder.setTitle(ctx.getString(R.string.dialog_insert_self_phonenumber_request_title));
    
    builder.setPositiveButton(ctx.getString(R.string.save),
        new Dialog.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                String toSavePhoneNumber = savedPhoneNumber.getText().toString().trim();
    
                Database thisDBHandler = new Database(ctx);
                thisDBHandler.open();
                thisDBHandler.insertSetting(RuntimeVars.getDBPhoneNumberName(), toSavePhoneNumber);
                thisDBHandler.close();
            }
        }
    );
    builder.setView(layout);
    return builder.create();
    

    So the last step should be to tell the OnClickListener() which context to use. But how can this be done? new Dialog.OnClickListener() won’t take any parameter about the context?

    Thanks!
    S.

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

Sidebar

Related Questions

No related questions found

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.