MainActivity 的代碼
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
/**
* 這個需要sd卡的讀取權限
*/
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
TextView tv;
Button btn;
private boolean flag;
private String tag;
private int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
*這三句代碼最好是寫在activity的基類中這樣才能捕獲整個應用的異常
*不能寫在MyApplication類裏,因爲對話框所需要的上下文要是activity的上下文
*/
//獲得UncaughtException捕獲異常類
UncaughtException mUncaughtException = UncaughtException.getInstance();
//初始化,把當前對象設置成UncaughtExceptionHandler處理器
mUncaughtException.init();
//傳遞對話框所需要的上下文
mUncaughtException.setContext(this);
flag=false;
btn= (Button) findViewById(R.id.btn);
tv= (TextView) findViewById(R.id.tv);
btn.setOnClickListener(this);
tv.setOnClickListener(this);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.tv:
break;
case R.id.btn:
//拋出異常測試
String str=null;
tv.setText(str.toString());
break;
}
}
}