[Java] When exception occurs, waiting for debugger is redisplayed and Logcat exception cannot be seen.

3 minute read

-The problem has not been resolved, but the cause has been identified, so please refer to it if you like.

  • Please note that it may be incorrect because it is written by a person who does not have much experience in application development.

When an exception occurred in Android studio, there was a problem that waiting for debugger came out and Logcat disappeared even if I tried to see the exception at that time.

This waiting for debugger is done every time you start debugging.

There are times when it does not come out, so when I tried a little under what conditions it came out, it came out under the following conditions. The exception is

  int[] i = null;
  for(int a:i){
  }

And I accessed a null int array by extended for and generated RuntimeException.

Since there are times when Logcat is visible and times when it is not visible, I tried to see under what conditions it was visible and under what conditions it was as follows. Premise

  1. Create 3 activities (Activity1, Activity2, Activity3)
  2. Create a button for Activity1 and Activity2 and make the screen transition when you press the button (Activity1 → Activity2 → Activity3).

When visible: An exception occurs in Activity1 and Activity2. When you can’t see: Exception occurred in Activity3. Here is the concrete source (a pattern where Logcat cannot be seen due to waiting for debugger).

Activity1

public class Activity1 extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ConstraintLayout constraintLayout =findViewById(R.id.baseLayout);

        Button button = new Button(this);
        button.setText("to 2nd");
        button.setOnClickListener((v)->{
            Intent intent = new Intent(this,Activity2.class);
            startActivity(intent);
        });
        constraintLayout.addView(button);
    }
}

Activity2

public class Activity2 extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ConstraintLayout constraintLayout =findViewById(R.id.baseLayout);

        Button button = new Button(this);
        button.setText("to 3nd");
        button.setOnClickListener((v)->{
            Intent intent = new Intent(this,Activity3.class);
            startActivity(intent);
        });
        constraintLayout.addView(button);
    }
}

Activitiy3

public class Activity3 extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        int[] i = null;
        for(int a:i){// throwing an exception here
        }
    }
}

If you bring this exception to Activity1 and Activity2, you can see Logcat without waiting for debugger. (The source is omitted because it becomes redundant)

And, after the exception that the waiting for debugger comes out was dropped, I built it that the application was up again.

I was just looking at waiting for debugger, but when I looked closely, when an exception occurred in Activity 3, it fell and the screen of Activity 1 was rising again.

Therefore, when I arrived at the place where “There is a problem with restarting this application”, I searched for “Android exception will restart” etc. and found that here/questions/28406),

  1. When an exception occurs, Process disappears and the application ends, but Task survives.
  2. The activity that caused the exception disappears from the task, and the remaining activities are opened according to the task recording order.

I thought it might be related to this. And when an exception occurs in Activity1, I can see that the application drops without Activity because “Activity disappears from Task”, but I thought it would be awkward to drop an exception in Activity2.

And when I went around further, I found that if an exception occurred, I also destroyed the activity of the caller, and I got the point that everything was falling this time.

Summary I’ll add another Activity (Activity4) to explain Exception occurred in Activity1 → Application terminated Exception occurs in Activity2 → Application ends (caller and callee are discarded) Exception occurred in Activity3 → To Activity1 (caller and callee are discarded) Exception occurred in Activity4 → To Activity2 (caller and callee are discarded)

And I understood the cause, but I was already down m (_ _) m in terms of how to deal with it, so I will summarize it as a memorandum.

If you have a chance, you may be able to deal with seeing Logcat firmly without restarting the application.