Novice Summary of the development encountered errors and solutions, if not, welcome correction, if there is a better solution, also please enlighten.
One, dialog.show () caused by the android.view.windowmanager$badtokenexception error
Error log
android.view.windowmanager$badtokenexception:unable to add window-token [email protected] was not valid; Is your activity running?at Android.view.ViewRootImpl.setView (viewrootimpl.java:653) at Android.view.WindowManagerImpl.addView (windowmanagerimpl.java:326) at Android.view.WindowManagerImpl.addView ( windowmanagerimpl.java:224) at Android.view.windowmanagerimpl$compatmodewrapper.addview (WindowManagerImpl.java : 149) at Android.view.window$localwindowmanager.addview (window.java:558) at Android.app.Dialog.show (Dialog.java : 316)
Cause of error
The cause of the error is that dialog must have an activity as a window carrier at show, and the above log means that the activity that carries dialog has been destroyed and does not exist.
Solutions
1. Before the showAdditional award if (!isfinishing ()) {
Dialog.show ();
}
2, direct try catch (not recommended)
error log
android.view.windowmanager$badtokenexception:unable to add window – token null is Not for a application
Error reason
First talk about the use of context
dialog box It is part of our activity, and the dialog box is attached to our activity;
Getapplicationcontext () This method gets the context
Activity.this a subclass of the context
that is activity.this equivalent to the subclass of Getapplicationcontext ()
There must be a subclass of the parent class-there is no token
subclass there is not necessarily a parent class-there is token
This also has Activity.this and our getapplicationcontext ();
Most recommended: Activity.this
Workaround
Context recommended for most cases: Activity.this
II, Dialog.dismiss () Java.lang.IllegalArgumentException error caused by
error log
Java.lang.IllegalArgumentException:View not attached to window Managerat Android.view.WindowManagerGlobal.findViewLocked (windowmanagerglobal.java:383) at Android.view.WindowManagerGlobal.removeView (windowmanagerglobal.java:285) at Android.view.WindowManagerImpl.removeView (windowmanagerimpl.java:104) at Android.app.Dialog.dismissDialog ( dialog.java:332) at Android.app.Dialog.dismiss (dialog.java:315)
Cause of error
This error test is not measurable, I added a third-party error statistics to be found, because for some reason, the activity was killed and re-created
Often this kind of exception situation is, there is a time-consuming thread operation, need to display a progressdialog, at the beginning of the task to display a dialog box, and then when the task is completed and then dismiss dialog box, If during this period if the activity was killed for some reason and restarted, then when dismiss WindowManager check that the activity that dialog belongs to does not exist, So the Illegalargumentexception:view not attached to Window manager will be reported.
Solutions
From the Internet to find a number of solutions are not too ideal, and then try to solve their own, I am so resolved, anyway, plus after this error will not appear again, if there is no more please enlighten.
Rewrite the activity's OnDestroy, and set dialog to null.
@Overridepublic void OnDestroy () {Super.ondestroy ();d ialog=null;}
Third, when reading the address book, the user chooses to reject, cannot obtain the permission to cause the Java.lang.SecurityException:Permission denial error
Error log
Java.lang.SecurityException:Permission denial:reading com.android.providers.contacts.ContactsProvider2 URI content ://com.android.contacts/data/phones from pid=27697, uid=10194 requires Android.permission.READ_CONTACTS, or Granturipermission () at Android.os.Parcel.readException (parcel.java:1465) at Android.database.DatabaseUtils.readExceptionFromParcel (databaseutils.java:185) at Android.database.DatabaseUtils.readExceptionFromParcel (databaseutils.java:137) at Android.content.ContentProviderProxy.query (contentprovidernative.java:413) at Android.content.ContentResolver.query (contentresolver.java:470) at Android.content.ContentResolver.query ( contentresolver.java:413)
Cause of error
User chooses reject when reading address book, failed to get permission
Solutions
Direct Try catch If an exception is caught, the user is not granted permission.
Four, when making a phone call, the phone does not have the relevant application caused by the android.content.ActivityNotFoundException error, the browser to open the page link, if not installed browser, will also produce similar errors, the solution is the same
Error log
Android.content.ActivityNotFoundException:No Activity found to handle Intent {act=android.intent.action.dial Dat=tel: Xxxxxxxxxxxx}at Android.app.Instrumentation.checkStartActivityResult (instrumentation.java:1632) at Android.app.Instrumentation.execStartActivity (instrumentation.java:1424) at Android.app.Activity.startActivityForResult (activity.java:3438) at Android.app.Activity.startActivityForResult ( activity.java:3399)
Cause of error
Because the phone does not have an app to call
Solutions
Direct Try catch If an exception is caught, prompting the user to handle this operation without the relevant application
V. In a child thread, update the UI
Error log
Android.view.viewrootimpl$calledfromwrongthreadexception:only the original thread that created a view hierarchy can Touc H its views.at android.view.ViewRootImpl.checkThread (viewrootimpl.java:5281) at Android.view.ViewRootImpl.requestLayout (viewrootimpl.java:943) at Android.view.View.requestLayout (View.java : 15614) at Android.view.View.requestLayout (view.java:15614)
Cause of error
When a toast is displayed on a child thread, the update UI can only be performed in the main thread
Solutions
1. Using Looper
Looper.prepare (); Toast.maketext (aactivity.this, "test", Toast.length_short). Show (); Looper.loop ();
2. Using handler
Defining in a class
Private final Handler Msghandler = new Handler () {public void Handlemessage (Message msg) { switch (MSG.ARG1) {
case r.string.msg_not_network: toast.maketext (Getapplicationcontext (), Getresources (). GetString ( r.string.msg_not_network), Toast.length_short). Show (); break; Default: Break;}} ;
in a child thread, send a message
Message msg = Msghandler.obtainmessage (); msg.arg1 = R.string.msg_not_network;msghandler.sendmessage (msg);
Summary of errors and solutions encountered in Android development