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

  • Home
  • SEARCH
  • 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 8699257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:54:21+00:00 2026-06-13T01:54:21+00:00

My OnPreferenceClickListener creates a new AlertDialog like below. When I run the program I

  • 0

My OnPreferenceClickListener creates a new AlertDialog like below. When I run the program I get the expection

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

What is wrong in my code?

public static class PrefsFragment extends PreferenceFragment {
    Preference pref= findPreference("text_preference1");
    pref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            new AlertDialog.Builder(mContext).setTitle(R.string.alert_dialog_title)
                    .setMessage(R.string.alert_dialog_message)
                    .setPositiveButton(R.string.alert_dialog_ok,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                        }
                    })
                    .setNegativeButton(R.string.alert_dialog_nein, null).show();

                    //do s.th.
        return false;
        }
    });
}
  • 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-06-13T01:54:22+00:00Added an answer on June 13, 2026 at 1:54 am

    I think you’re getting the ApplicationContext. But it shouldn’t be used for creating Dialogs.

    Instead of mContext in new AlertDialog.Builder(mContext) you should use getActivity(), which returns the activity associated with a fragment.

    Since you are using fragments get the Activity’s Context simply by calling the Fragments getActivity() method.

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

Sidebar

Related Questions

I have set an onPreferenceClickListener on my subclass of DialogPreference like so: public class
Preference customPreference = (Preference) findPreference(customPreference); customPreference.setOnPreferenceClickListener(new onPreferenceClickListener)(){ public boolean onPreferenceClick(Preference preference){ return false; }
I defined my preferences in xml file like the following: <PreferenceScreen xmlns:android=http://schemas.android.com/apk/res/android> <PreferenceScreen android:title=@string/pref_screen
I have a OnPreferenceClickListener which should remove a specific Preference (with key preference_to_remove )
just wanted to add this Color Picker ( http://code.google.com/p/devmil-android-color-picker/source/browse/ ) to my app and
I have an PreferenceActivity where I would like to add Preferences dynamically. On a
I am creating a preference menu and would like to launch a browser (with
I have dialog layout xml file which stores button, and I have set android:onClick=doSomething
Can you tell me why this click listener crashes the Android device when the
In my app I'd like to show a warning when using a certain preference

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.