java.lang.NoClassDefFoundError: android.util.Patterns
android.util.Patterns
class is available starting from API version 8 (Android 2.2.x), and the user had version 2.1. We decided this of course by wrapping this code in try/catch
. android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077) at java.net.InetAddress.lookupHostByName(InetAddress.java:477) at java.net.InetAddress.getAllByNameImpl(InetAddress.java:277) at java.net.InetAddress.getAllByName(InetAddress.java:249)
Activity
starts (in the onCreate()
method), we start the stream (not to block the UI stream), which will load the data. This thread runs for a while, so we will display the boot process in the ProgressDialog
. Everything is simple and works great.ProgressDialog
appeared again and we are loading our data again. But nothing has changed? It's just easier for a person to look at this list by turning the device.onCreate()
method is called not only when creating an Activity
, but also when you rotate the screen! But we do not want to make the user wait for the download again. All we need is to show the already loaded data again.onConfigurationChanged()
! This is not true! Official documentation clearly states that “ using this attribute should not be avoided. “ Http://developer.android.com/guide/topics/manifest/activity-element.html#configonRetainNonConfigurationInstance()
method of your Activity
. And you can return data (for example, a list of loaded objects) from this method, which will be saved between 2 calls of the onCreate()
method of your Activity
. Then when you call onCreate()
you can call getLastNonConfigurationInstance()
which will return the saved data to you. Those. when creating an Activity
you call getLastNonConfigurationInstance()
and if it returned data to you, then this data has already been loaded and you only need to display it. If you did not return the data, then start the download.ProgressDialog
displayed) or the data has already been loaded and we saved it in the list for display. It turns out that in the first case, when turning, we must save the link to the working thread, and in the second case, the link to the list that has already been downloaded. We do that.ProgressDialog
again with onCreate()
! And if we add here that in our application the user can download data from different places and we have more than one data download stream, but several — the amount of code that serves a simple screen rotation becomes huge.onPreExecute()
and onPostExecute(Result)
, which are executed in the main UI thread and which serve to display something and there is a method doInBackground (Params ...) within which the main work and it starts in a separate thread automatically. Here is an example code like this: private class MyTask extends AsyncTask<Void, Void, Void> { private ProgressDialog spinner; @Override protected void onPreExecute() { // ProgressDialog // // UI spinner = new ProgressDialog(MyActivity.this); spinner.setMessage(" ..."); spinner.show(); } @Override protected Void doInBackground(Void... text) { // // } @Override protected void onPostExecute(Void result) { // . ProgressDialog. // UI spinner.dismiss(); } }
android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@40515bd0 is not valid; is your activity running? at android.view.ViewRoot.setView(ViewRoot.java:534) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) at android.view.Window$LocalWindowManager.addView(Window.java:424) at android.app.Dialog.show(Dialog.java:241) at ru.reminded.android.social.SocialDataLoader.onPreExecute(SocialDataLoader.java:106) at android.os.AsyncTask.execute(AsyncTask.java:391) at ru.reminded.android.util.SocialAdapterUtils.loadAdapterData(SocialAdapterUtils.java:52) at ru.reminded.android.util.SocialAdapterUtils.access$0(SocialAdapterUtils.java:50) at ru.reminded.android.util.SocialAdapterUtils$1.onComplete(SocialAdapterUtils.java:41)
spinner.show()
in the onPreExecute()
method, this ProgressDialog
created with reference to MyActivity
already inactive and is not on the screen! Well, I still understand how this can be when calling onPostExecute()
. Those. for example, while we were loading data, the user clicked Back and our ProgressDialog
left the screen. But how can this be immediately when a load is called, when this code starts right after the activity is closed - this is unclear to me.spinner.show()
and spinner.dismiss()
methods in try/catch
. The solution is certainly not very beautiful, but in our case it is quite functional.ProgressDialog
. We had to add processing to their code. So this problem is not only in us.ProgressDialog
if there was a turn during data loading. This will also add auxiliary code here.doInBackground()
method is executed in a separate thread and therefore if an error occurred while loading data, then you cannot issue an Alert directly from there, since this is not a UI stream. It is necessary to save the error, and then after exiting the load stream in the onPostExecute(Void result)
method, you can already show something. android.util.AndroidRuntimeException: { what=1004 when=-14ms arg2=1 } This message is already in use. at android.os.MessageQueue.enqueueMessage(MessageQueue.java:187) at android.os.Handler.sendMessageAtTime(Handler.java:457) at android.os.Handler.sendMessageDelayed(Handler.java:430) at android.os.Handler.sendMessage(Handler.java:367) at android.os.Message.sendToTarget(Message.java:349) at android.webkit.WebView$5.onClick(WebView.java:1250) at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:172) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:130) at android.app.ActivityThread.main(ActivityThread.java:3703) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) at dalvik.system.NativeStart.main(Native Method)
java.lang.NullPointerException at android.os.Message.sendToTarget(Message.java:348) at android.webkit.WebView$4.onClick(WebView.java:1060) at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:123) at android.app.ActivityThread.main(ActivityThread.java:4627) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:521) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) at dalvik.system.NativeStart.main(Native Method)
java.lang.IndexOutOfBoundsException: setSpan (-1 ... -1) starts before 0 at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:949) at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:522) at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:514) at android.text.Selection.setSelection(Selection.java:74) at android.text.Selection.setSelection(Selection.java:85) at android.widget.TextView.performLongClick(TextView.java:8621) at android.webkit.WebTextView.performLongClick(WebTextView.java:617) at android.webkit.WebView.performLongClick(WebView.java:4471) at android.webkit.WebView$PrivateHandler.handleMessage(WebView.java:8285) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:150) at android.app.ActivityThread.main(ActivityThread.java:4389) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) at dalvik.system.NativeStart.main(Native Method)
try/catch
. Even where it is not at all obvious.Source: https://habr.com/ru/post/146632/
All Articles