What is the difference between ANR and crash in Android?

前端 未结 13 2161
北海茫月
北海茫月 2020-12-07 22:56

I have searched on the internet regarding what an ANR is. And I studied those references as well. But I don\'t get details regarding a crash in Android.

Can someone

相关标签:
13条回答
  • 2020-12-07 23:18

    ANR stands for Application Not Responding.

    An ANR will occur if you are running a process on the UI thread which takes a long time, usually around 5 seconds. During this time the GUI (Graphical User Interface) will lock up which will result in anything the user presses will not be actioned. After the 5 seconds approx has occurred, if the thread still hasn't recovered then an ANR dialogue box is shown informing the user that the application is not responding and will give the user the choice to either wait, in the hope that the app will eventually recover, or to force close the app.

    A crash is when an exception within the app has been thrown which has not been handled. For example, if you try to set the text of an EditText component, but the EditText is null and there is no try catch statement to catch the exception that your app will crash and will be force closed. The user will not see what caused the crash, they will be shown a dialogue telling that the app has force closed unexpectedly and will give them the option to send a bug report. In this example if you were to look in the bug report you would see the error caused by java.lang.NullPointerException.

    Hope this helps.

    0 讨论(0)
  • 2020-12-07 23:20

    ANR and Crash Examples:

    This question already has an accepted answer, but I am adding 2 simple examples to understand ANR and Crash better.

    ANR:

    // this will produce an ANR on your app
    int i = 0;
    while(true) {
        i++;
    }
    

    Crash:

    // this will crash your app : will produce java.lang.ArithmeticException
    int value = 5, i = 0;
    int result = value / i;
    
    0 讨论(0)
  • 2020-12-07 23:24

    ANR stands for Application Not Responding.

    It can occur due to many reasons like if an application blocks on some I/O operation on the UI thread so the system can’t process incoming user input events. Or perhaps the app spends too much time building an elaborate in-memory structure or computing the next move in the UI thread.

    Blocking the main thread, won't result in a crash, but a popup will be displayed to let users kills the app after 5 seconds.

    But For Crash, the main reason is the human errors. Most of the time an app crashes is because of a coding/design error made by human

    Human Errors

    Lack of testing

    Null Pointer exception

    OutofMemory

    Example :

    This is common when a programmer makes a reference to an object or variable that does not exist, basically creating a null-pointer error.

    If you have a bad connection, that can also make your apps crash. The app could also have memory management problems.

    Please see my answer for the type of android specific exception which may cause the crash.

    Android Specific Exception

    0 讨论(0)
  • 2020-12-07 23:27
    ANR: It is called when anything your application is doing in the UI thread that 
    takes a long time to complete (5 sec approx)
    

    Reference: ANR

    Crash: It is called when  your Application gets some Error or Exception raised by the DVM
    
    0 讨论(0)
  • 2020-12-07 23:28

    ANR: Basically due to a long running task on main thread.

    There are some common patterns to look for when diagnosing ANRs:

    1. The app is doing slow operations involving I/O on the main thread.
    2. The app is doing a long calculation on the main thread.
    3. The main thread is doing a synchronous binder call to another process, and that other process is taking a long time to return.
    4. The main thread is blocked waiting for a synchronized block for a long operation that is happening on another thread.
    5. The main thread is in a deadlock with another thread, either in your process or via a binder call. The main thread is not just waiting for a long operation to finish, but is in a deadlock situation.

    The following techniques can help you find out which of these causes is causing your ANRs.

    CRASH:

    Reason for crashs can be many. Some reasons are obvious, like checking for a null value or empty string, but others are more subtle, like passing invalid arguments to an API or even complex multithreaded interactions

    0 讨论(0)
  • 2020-12-07 23:29

    [ANR and Crash Different][1] Android applications normally run entirely on a single thread by default the “UI thread” or “main thread”. This means anything your application is doing in the UI thread that takes a long time to complete can trigger the ANR dialog because your application is not giving itself a chance to handle the input event or intent broadcasts.

    0 讨论(0)
提交回复
热议问题