Summary of errors encountered during android development and Solutions

Source: Internet
Author: User

Summary of errors encountered during android development and Solutions
If any errors or solutions have been encountered during the Development summarized by the novice, please correct them. If you have any better solutions, please kindly advise.

1. android. view. WindowManager $ BadTokenException error caused by dialog. show ()

Error Log

android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@427b7270 is 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)

Error cause
The error is that when Dialog is displayed, an activity must be used as the window carrier. The preceding log indicates that the activity carrying the Dialog has been destroyed and does not exist.

Solution
1. Add a check before show to check whether the activity has been destroyed.
If (! IsFinishing ()){
Dialog. show ();
}
2. try catch directly (not recommended)
Error Log
Android. view. WindowManager $ BadTokenException: Unable to add window -- token null is not for an application
Error cause
First, let's talk about the use of context.
The dialog box is a part of our Activity. It is mounted to our Activity;
The getApplicationContext () method obtains the Context
Activity. this gets a subclass of Context.
That is to say, Activity. this is equivalent to the subclass of getApplicationContext ().
Some subclass of the parent class must have-no token
Some sub-classes do not necessarily have parent classes -- they have token
This also includes Activity. this and our getApplicationContext ();
Recommended in most cases: Activity. this
Solution
The context is recommended in most cases: Activity. this

Ii. java. lang. IllegalArgumentException error caused by dialog. dismiss ()

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)


Error cause
This error test cannot be tested. I added the third-party error statistics to discover the error. The reason is that the Activity is killed and re-created due to some reason.
Normally, this type of Exception occurs when there is a time-consuming thread operation. You need to display a ProgressDialog and a dialog box at the beginning of the task. Then, when the task is completed, the Dismiss dialog box is displayed, if the Activity is killed for some reason and restarted during this period, when Dismiss occurs, WindowManager checks and finds that the Activity to which Dialog belongs does not exist, so IllegalArgumentException is reported: view not attached to window manager.
Solution
I found some solutions on the Internet, but they were not ideal. Then I tried to solve it myself. I did this. I did not see this error after I added it, if any, please kindly advise.
Override onDestroy of Activity and set dialog to null.
@Overridepublic void onDestroy() {super.onDestroy();dialog=null;}



Iii. java. lang. SecurityException: Permission Denial error caused by the user's refusal to read the address book.
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)


Error cause
When reading the address book, the user selects a denial and fails to obtain the permission.
Solution
If an exception is caught, try catch prompts that the user has not granted the permission.

4. When you make a call, the mobile phone does not have the android app. content. activityNotFoundException error. When you open a webpage link in a browser, a similar error will occur if the browser is not installed. 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)


Error cause
Because the mobile phone does not have an application that can call the phone.
Solution
Try catch directly. If an exception is caught, the system prompts the user that there is no relevant application to handle this operation.
5. Update the UI in the sub-Thread
Error Log
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch 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)


Error cause
Display a Toast in the sub-thread. The UI update can only be performed in the main thread.
Solution
1. Use Logoff
Looper.prepare();Toast.makeText(aActivity.this,"test",Toast.LENGTH_SHORT).show();Looper.loop();


2. Use Handler
Define in 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;                } }};


Send messages to sub-threads
Message msg = msgHandler.obtainMessage();msg.arg1 = R.string.msg_not_network;msgHandler.sendMessage(msg);

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.