How to crash an Android app programmatically?

后端 未结 13 1485
遥遥无期
遥遥无期 2021-02-05 00:37

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?

13条回答
  •  伪装坚强ぢ
    2021-02-05 00:48

    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..

    Eg:

    Button b;
    
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            b = (Button)findViewById(R.id.butt);
            b.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View arg0) {
    
    
            }
        });
            setContentView(R.layout.main);
    
    
        }
    

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

提交回复
热议问题