[JAVA] In case of exception, waiting for debugger is redisplayed and Logcat exception cannot be seen.

・ The problem has not been solved yet, but the cause has been identified, so please refer to it if you like. ・ Because it is written by a person who does not have much experience in application development, please refer to it after careful consideration that it may be incorrect.

When an exception occurred in Android studio, waiting for debugger appeared, and even if I tried to see the exception at that time, Logcat disappeared and I could not see it.

This is waiting for debugger, but every time I start debugging.

There are times when it is not the time to 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){
  }

I accessed a null int array with extended for and raised a RuntimeException.

Since there are times when Logcat can be seen and times when it cannot be seen, when I tried to see under what conditions it was visible and under what conditions it was not visible, the conditions were as follows. Premise

    1. Create 3 Activities (Activity1, Activity2, Activity3)
  1. Create buttons in Activity1 and Activity2 so that the screen changes when the button is pressed (Activity1 → Activity2 → Activity3).

When visible: Exception occurred in Activity1 and Activity2. When not visible: Exception occurred in Activity3. Here is the specific source (a pattern where waiting for debugger appears and Logcat cannot be seen).

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){//I'm raising an exception here
        }
    }
}

If you bring this exception to Activity1 and Activity2, the waiting for debugger will not appear and you will be able to see Logcat. (The source is omitted because it becomes redundant)

And, the exception is that waiting for debugger appears, and after that, the app is launched again.

I was only looking at the waiting for debugger, but when I looked closely, when an exception occurred in Activity3, it fell and the screen of Activity1 was launched again.

Therefore, when I arrived at the place where "There is a problem with restarting this application" and searched for "android exception restarts" etc., here of Terratail I arrived at the question of / questions / 28406),

  1. When an exception occurs, Process disappears and the application ends, but Task survives.
  1. The activity in which the exception occurred disappears from the Task, and the remaining Activities are opened according to the recording order of the Task.

I thought that it might be related to this. And when an exception occurs in Activity1, "Activity disappears from Task", so I understand that there is no Activity and the application crashes, but I wondered what it was like to drop even if I put an exception in Activity2.

Then, after going further, I found out that if an exception occurs, the activity of the caller is also destroyed, and all the points have come to the point of this time.

Summary I will explain by adding another Activity (Activity4). Exception occurs in Activity1 → App ends Exception occurs in Activity2 → App ends (caller and callee are discarded) Exception occurs in Activity3 → Go to Activity1 (caller and callee are discarded) Exception occurs in Activity4 → Go to Activity2 (caller and callee are discarded)

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

If there is a chance, I may deal with it until I can see Logcat firmly without restarting the application.

Recommended Posts

In case of exception, waiting for debugger is redisplayed and Logcat exception cannot be seen.
"ClassNotFoundException" is displayed when tomcat is started in eclipse and cannot be started
The strong parameter is true, but for some reason it cannot be saved in the table.