How can I prevent my Android application from crashing if any exception happens in the activity code or service code in Java?
Here is the code example of an unhanded Exception from an Activity or Service:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Integer.parseInt("error");
}
When this code is run, it crashes and produces the "Unfortunately, App has stopped" message.