Home » Android » java – How to crash an Android app programmatically?

java – How to crash an Android app programmatically?

Posted by: admin April 23, 2020 Leave a comment


I want to test out crash report using acra but the first step is I need to simulate a fatal crash in Android using code.

Any idea?

How to&Answers:
Just execute this code: divide by zero

Update: Also can try this
Create a method,

public void stackOverflow() {


And call this somewhere/buttonClick

OR simply throw an uncaught exception

throw new RuntimeException("This is a crash");



  1. Access a view that is not defined.
  2. Access the first element of an empty list without checking.
  3. Divide by Zero.
  4. Throw the device out the window.
  5. Submerge the device in water.

By the way this IS a silly question.


Don’t declare activity in the Android Manifest .


You could try a Null Pointer exception.

Integer i = null;

Then invoke any method on the object.


Invoking a method on an object that hasn’t been initialized will crash the app.


A very simple approach… and is very important to understand that why it happened.

Try initiating a variable in onCreate() before the setContentView() method, then use it to call a method or variable or try registering it to some listener..


Button b;

public void onCreate(Bundle savedInstanceState) {

        b = (Button)findViewById(R.id.butt);
        b.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {



This crashed, because before setContentView() none of the components/view in the main.xml layout got their ids.


Simply create in your main.xml a button like this:

    android:text="Crash me" />

then run your app and click for crash


Most simple I know : throw null.

You can’t throw null, so a NullPointerException is raised.

throw null;


you can crash with a simple null point exception.

 throw new NullPointerException();


in addition to @vinnet-shukla answer:

“OR simply throw an uncaught exception”

throwing uncaught exception to do a crash is bad idea as the exception could by caught somwhere higher in the stack – especially when whe don’t know where we are right now 🙂

more ellegant way is to use ERROR

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. The ThreadDeath error, though a “normal” condition, is also a subclass of Error because most applications should not try to catch it.

so we could make our own Error subclass and use it:

 * usage:
 *   CrashError.doCrash(); 
public class CrashError extends Error {

    public CrashError() { 
        this("simulated crash");

    public CrashError(String msg) {

    public static void doCrash() {
        throw new CrashError();


but if we talk about other possibilities we could also a throw checked exception 🙂

this will also be a lesson how we could RETHROW CHECKED EXCEPTION 🙂 with other way than use of sun.misc.Unsafe especially when the class is not available in VM implementation

public static <E extends Throwable> void throwAnyT(Throwable e) throws E {
    throw (E) e;

public static void throwUnchecked(Throwable e) {
    // should never get there.
    throw new InternalError();

public static void crash() {
    throwUnchecked(new java.io.IOException("simulated crash"));

in addition to @audric answer:

“You can’t throw null”

yes you can 🙂 it’s exactly what you are doing in your example and yes it could get undetectable if the catch block will not use Throwable – the NPX will never be thrown and simply there will be normal flow when code will still execute and yes i’ll have seen this and experienced by myself 🙂 on ANDROID DALVIK

and what about..? maybe it could fulfill your needs?

java specific (also in android):

- Runtime.getRuntime().exit(int);
- System.exit(int);
- Runtime.getRuntime().halt(int);

android specific:

- android.os.Process.killProcess(int);
- android.os.Process.killProcessQuiet(int);
- android.os.Process.sendSignal(int,int);
- android.system.Os.kill(int);


Use the following code:

String xyz=null;