当我的应用程序崩溃时如何设置自定义对话框?
How do I set custom Dialog Box when my App has crashed?
我正在我的应用程序中使用 ACRA 4.9.0
。我只想在我的应用程序崩溃时向用户显示对话框。
这是我当前的代码:
public class App extends MultiDexApplication {
@Override
public void onCreate() {
ConfigurationBuilder config = new ConfigurationBuilder(this);
config.setMailTo("mailto@gmail.com");
config.setResDialogIcon(android.R.drawable.ic_dialog_info);
config.setResDialogText(R.string.crash_dialog_text);
config.setResDialogTitle(R.string.crash_dialog_title);
config.setResDialogCommentPrompt(R.string.crash_dialog_comment_prompt);
config.setResDialogOkToast(R.string.crash_dialog_ok_toast);
config.setMode(ReportingInteractionMode.DIALOG);
try{
config.setMode(ReportingInteractionMode.DIALOG);
} catch (Exception e) {
e.printStackTrace();
return;
}
ACRA.init(this, config);
super.onCreate();
Instance = this;
applicationHandler = new Handler(getInstance().getMainLooper());
}
完整的演示
申请class
@ReportsCrashes(formUri = "http://yourserver.com/yourscript",
mode = ReportingInteractionMode.DIALOG,
resToastText = R.string.app_name,
resDialogText = R.string.app_name,
resDialogIcon = android.R.drawable.ic_dialog_info,
resDialogTitle = R.string.app_name,
resDialogCommentPrompt = R.string.app_name,
resDialogEmailPrompt = R.string.app_name,
resDialogOkToast = R.string.app_name,
resDialogTheme = R.style.AppTheme_Dialog)
public class App extends Application {
private Context context;
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
ACRA.init(this);
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
EditText number = (EditText) findViewById(R.id.foodName);
number.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int a = 0 / 0;
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/base"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:background="@color/white"
android:orientation="horizontal"
android:weightSum="10">
<EditText
android:id="@+id/foodName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="64dp"
android:layout_marginRight="64dp"
android:hint="Food name"
android:inputType="textCapWords"
android:textColor="@color/colorPrimaryDark"
android:textColorHint="@color/colorPrimaryDark"
android:textSize="32sp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
对话:
我正在我的应用程序中使用 ACRA 4.9.0
。我只想在我的应用程序崩溃时向用户显示对话框。
这是我当前的代码:
public class App extends MultiDexApplication {
@Override
public void onCreate() {
ConfigurationBuilder config = new ConfigurationBuilder(this);
config.setMailTo("mailto@gmail.com");
config.setResDialogIcon(android.R.drawable.ic_dialog_info);
config.setResDialogText(R.string.crash_dialog_text);
config.setResDialogTitle(R.string.crash_dialog_title);
config.setResDialogCommentPrompt(R.string.crash_dialog_comment_prompt);
config.setResDialogOkToast(R.string.crash_dialog_ok_toast);
config.setMode(ReportingInteractionMode.DIALOG);
try{
config.setMode(ReportingInteractionMode.DIALOG);
} catch (Exception e) {
e.printStackTrace();
return;
}
ACRA.init(this, config);
super.onCreate();
Instance = this;
applicationHandler = new Handler(getInstance().getMainLooper());
}
完整的演示
申请class
@ReportsCrashes(formUri = "http://yourserver.com/yourscript",
mode = ReportingInteractionMode.DIALOG,
resToastText = R.string.app_name,
resDialogText = R.string.app_name,
resDialogIcon = android.R.drawable.ic_dialog_info,
resDialogTitle = R.string.app_name,
resDialogCommentPrompt = R.string.app_name,
resDialogEmailPrompt = R.string.app_name,
resDialogOkToast = R.string.app_name,
resDialogTheme = R.style.AppTheme_Dialog)
public class App extends Application {
private Context context;
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
ACRA.init(this);
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
EditText number = (EditText) findViewById(R.id.foodName);
number.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int a = 0 / 0;
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/base"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:background="@color/white"
android:orientation="horizontal"
android:weightSum="10">
<EditText
android:id="@+id/foodName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="64dp"
android:layout_marginRight="64dp"
android:hint="Food name"
android:inputType="textCapWords"
android:textColor="@color/colorPrimaryDark"
android:textColorHint="@color/colorPrimaryDark"
android:textSize="32sp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
对话: