public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView v= null; v.setText("abc"); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; }}
在application中加入acra
@ReportsCrashes(formKey="dGVacG0ydVHnaNHjRjVTUTEtb3FPWGc6MQ",mailTo = "fenghanlu@163.com",mode = ReportingInteractionMode.DIALOG,customReportContent = { ReportField.APP_VERSION_NAME, ReportField.ANDROID_VERSION, ReportField.PHONE_MODEL, ReportField.CUSTOM_DATA,ReportField.BRAND, ReportField.STACK_TRACE, ReportField.LOGCAT,ReportField.USER_COMMENT },resToastText = R.string.crash_toast_text, // optional, displayed as soon as the crash occurs, before collecting data which can take a few secondsresDialogText = R.string.crash_dialog_text,resDialogIcon = android.R.drawable.ic_dialog_info, //optional. default is a warning signresDialogTitle = R.string.crash_dialog_title, // optional. default is your application nameresDialogCommentPrompt = R.string.crash_dialog_comment_prompt, // optional. when defined, adds a user text field input with this text resource as a labelresDialogOkToast = R.string.crash_dialog_ok_toast // optional. displays a Toast message when the user accepts to send a report.)public class TestApplication extends Application{@Overridepublic void onCreate() {super.onCreate();ACRA.init(this);ErrorReporter.getInstance().removeAllReportSenders();ErrorReporter.getInstance().setReportSender(new CrashReportSender(getApplicationContext()));//ErrorReporter.getInstance().setReportSender(new AcraSender(getApplicationContext(),"http://10.32.202.155:9876"));}}
自訂sender
ublic class CrashReportSender implements ReportSender{private final Context mContext;public final static String formUri="http://10.32.202.155:9876";public CrashReportSender(Context mContext) {super();this.mContext = mContext;ACRA.getErrorReporter().putCustomData("PLATFORM", "ANDROID");ACRA.getErrorReporter().putCustomData("BUILD_ID", android.os.Build.ID);ACRA.getErrorReporter().putCustomData("DEVICE_NAME", android.os.Build.PRODUCT);}@Overridepublic void send(CrashReportData arg0) throws ReportSenderException {EmailIntentSender emailSender = new EmailIntentSender(mContext); emailSender.send(arg0); // send crash report to host HttpPostSender httpSender = new HttpPostSender(formUri, null); httpSender.send(arg0); //send crash report to google GoogleFormSender googleSender =new GoogleFormSender(); googleSender.send(arg0);}
AndroidManifest.xml中加入註冊
<activity android:name="org.acra.CrashReportDialog" android:excludeFromRecents="true" android:finishOnTaskLaunch="true" android:launchMode="singleInstance" android:theme="@android:style/Theme.Dialog" />
註:acra版本必須是4.4.0或者更高版本