Android 1.6: “android.view.WindowManager$BadTokenException: Unable to add window — token null is not for an application”

后端 未结 16 1159
滥情空心
滥情空心 2020-11-22 07:46

I\'m trying to open a dialog window, but every time I try to open it it throws this exception:

Uncaught handler: thread main exiting due to uncaught exceptio         


        
相关标签:
16条回答
  • 2020-11-22 07:56

    For nested dialogs this issue is very common, It works when

    AlertDialog.Builder mDialogBuilder = new AlertDialog.Builder(MyActivity.this);
    

    is used instead of

    mDialogBuilder = new AlertDialog.Builder(getApplicationContext);
    

    this alternative.

    0 讨论(0)
  • 2020-11-22 07:57

    This Worked for me--

    new AlertDialog.Builder(MainActivity.this)
            .setMessage(Html.fromHtml("<b><i><u>Spread Knowledge Unto The Last</u></i></b>"))
            .setCancelable(false)
            .setPositiveButton("Dismiss",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                        }
                    }).show();
    

    Use

    ActivityName.this
    
    0 讨论(0)
  • 2020-11-22 07:58

    You can also do this

    public class Example extends Activity {
        final Context context = this;
        final Dialog dialog = new Dialog(context);
    }
    

    This worked for me !!

    0 讨论(0)
  • 2020-11-22 08:00

    Android documents suggests to use getApplicationContext();

    but it will not work instead of that use your current activity while instantiating AlertDialog.Builder or AlertDialog or Dialog...

    Ex:

    AlertDialog.Builder builder = new  AlertDialog.Builder(this);
    

    or

    AlertDialog.Builder builder = new  AlertDialog.Builder((Your Activity).this);
    
    0 讨论(0)
  • 2020-11-22 08:01

    Another solution is to set the window type to a system dialog:

    dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
    

    This requires the SYSTEM_ALERT_WINDOW permission:

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    

    As the docs say:

    Very few applications should use this permission; these windows are intended for system-level interaction with the user.

    This is a solution you should only use if you require a dialog that's not attached to an activity.

    0 讨论(0)
  • 2020-11-22 08:03
    public class Splash extends Activity {
    
        Location location;
        LocationManager locationManager;
        LocationListener locationlistener;
        ImageView image_view;
        ublic static ProgressDialog progressdialog;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.splash);
            progressdialog = new ProgressDialog(Splash.this);
               image_view.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
    
                            locationManager.requestLocationUpdates("gps", 100000, 1, locationlistener);
                            Toast.makeText(getApplicationContext(), "Getting Location plz wait...", Toast.LENGTH_SHORT).show();
    
                                progressdialog.setMessage("getting Location");
                                progressdialog.show();
                                Intent intent = new Intent(Splash.this,Show_LatLng.class);
    //                          }
            });
        }
    

    Text here:-
    use this for getting activity context for progressdialog

     progressdialog = new ProgressDialog(Splash.this);
    

    or progressdialog = new ProgressDialog(this);

    use this for getting application context for BroadcastListener not for progressdialog.

    progressdialog = new ProgressDialog(getApplicationContext());
    progressdialog = new ProgressDialog(getBaseContext());
    
    0 讨论(0)
提交回复
热议问题