Android self-use -- windowmanager $ badtokenexception: unable to add window-Token null is n

Source: Internet
Author: User

Error generation:

Java code {
DP. Sh. toolbar. copytoclipboard (this); Return false;
} "Href =" http://blog.csdn.net/zeng622peng/archive/2010/12/25/6097346.aspx# ">
  1. PrivateContext mcontext;
  2. @ Override
  3. Protected VoidOncreate (bundle savedinstancestate) {mcontext = getapplicationcontext ();
  4. System. Out. println ("mcontext =" + mcontext );
  5. }
Java code {
DP. Sh. toolbar. copytoclipboard (this); Return false;
} "Href =" http://blog.csdn.net/zeng622peng/archive/2010/12/25/6097346.aspx# ">
  1. NewAlertdialog. Builder (mcontext)
  2. . Seticon (Android. R. drawable. ic_dialog_alert)
  3. . Settitle ("warnning ")
  4. . Setmessage (
  5. "You forget to write the message. Do you want to fill out it ?? ")
  6. . Setpositivebutton ("yes", positivelistener). setnegativebutton (
  7. "No", negativelistener). Create (). Show ();

This error is reported in the new alertdialog. builder (mcontext), although the parameter here is alertdialog. builder (context), but we cannot use the context obtained by getapplicationcontext (). Instead, we must use activity, because only one activity can add a form.

Solution: use activity. This (activity is the name of your activity) to fill the parameters in new alertdialog. Builder (context) to create a correct dialog.

Java code {
DP. Sh. toolbar. copytoclipboard (this); Return false;
} "Href =" http://blog.csdn.net/zeng622peng/archive/2010/12/25/6097346.aspx# ">
  1. NewAlertdialog. Builder (myactivity.This)
  2. . Seticon (Android. R. drawable. ic_dialog_alert)
  3. . Settitle ("warnning ")
  4. . Setmessage (
  5. "You forget to write the message. Do you want to fill out it ?? ")
  6. . Setpositivebutton ("yes", positivelistener). setnegativebutton (
  7. "No", negativelistener). Create (). Show ();

 

Solve the problem!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.