Summary of common exceptions in Android development


In our development, we will inevitably encounter various Exceptions. Record and summarize again, I hope it will be helpful to you who are passing by.

Exception子子孙孙无穷尽也,我会不断新增内容补充完整,欢迎收藏,不断回顾

Java exception

1. ConcurrentModificationException

List<String> list = new ArrayList<>();
    list.add("di");
    String str = new String("da");
    list.add(str);
    list.add("paint");
    for(String temp : list){
    
    
        if(temp == "di" || temp == "da"){
    
    
            list.remove(temp);
        }
    }
System.out.print(list.toString());

会抛出ConcurrentModificationException异常,List不可在进行遍历时添加或移除其中的元素。

Source code analysis : There is a modCount member variable in the parent class AbstractList of ArrayList to record the number of modifications to the List, and an expectedModCount to record the expected value of the number of modifications to the ArrayList, modCount is its initial value.

At the beginning of the traversal, both modCount and expectedModCount are 0, and when we add or remove the List during the traversal, modCount will increase by 1, and the expectedModCount remains unchanged.
And List will first check whether modCount and expectedModCount are equal when fetching the next value through the next() method. If they are not equal, a ConcurrentModificationException will be thrown.

Single-threaded solution : Use Iterator to traverse the collection, call the Remove method of Iterator to remove elements, and there is an operation of expectedModCount = modCount inside.

Multi-threaded solution : using the above method in multi-threading will also throw this exception. You can use synchronized or Lock to synchronize when using iterator iteration, or use CopyOnWriteArrayList to replace ArrayList.

Summary
The addition and deletion of collection elements in the for loop requires special attention. The above provides two solutions for different thread situations. I added that using the for-- operation scheme can also operate normally.

Android exception

1. SourceSet with name ‘main’ not found

In Android, I want to test the code running, use the main method to test some method calls, and find that it cannot be used, and report the following error.
Insert picture description here
Solution:
Add a configuration parameter to the gradle.xml file in the project.idea folder.

   <option name="delegatedBuild" value="false" />

Insert picture description here

2. IllegalStateException: This Activity already has an action bar supplied by the window decor

Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
Insert picture description here
The translation of the error message is that the Window of the Activity has provided you with an ActionBar. If you want to use the toolbar, then you need to set windowActionBar=false in the theme.

The cause of the exception

  1. This is my theme configuration
    Insert picture description here
  2. This is my Activity’s layout

    Toolbar and actionBar cannot appear in the same Window, only one of the two

Solution
Because I need to use Toobar, I can only remove ActionBar.
Use Theme.AppCompat.Light.NoActionBar
Insert picture description here

3. java.io.IOException: unexpected end of stream on Connection 196.*******

This problem was encountered in Okhttp+retrofit. The exception was caused by the fact that the connection between the client and the server was not disconnected. This situation will occur in the next request. There are two solutions to this situation:

  1. After the server configures the client request successfully, disconnect the TCP connection with it
  2. Configure addHeader("Connection", "close").build(); in Retroift;
	OkhttpBuild .addNetworkInterceptor(getCacheIntercepter())
 
    private Interceptor getCacheIntercepter() {
    
    
        return chain -> {
    
    
//            Request request = chain.request();
			//上面的屏蔽化成下面一句
            Request request = chain.request().newBuilder().addHeader("Connection", "close").build();

The above two methods are the methods I have found by the deadline. If you have a better solution, please contact me.

4. Code does not display variable values ​​in Android Studio debug mode

Don't forget to configure debuggable=ture in the build.gradle file of the app. Otherwise, you will find that you cannot see the variable value of the breakpoint when you break it.
Insert picture description here

to sum up

In Android development, we will always encounter all kinds of weird Exceptions. Android development experience is based on solving bugs and solving Exceptions. When you are proficient in locating various Bugs and Exceptions, you can know the causes and effective After the solution, congratulations, you have taken another big step toward the altar of the god of technology.

Blog writing is not easy, if you think the article is okay, please like it ^ _ ^!

Guess you like

Origin blog.csdn.net/luo_boke/article/details/106349481