如何防止 android 应用崩溃
How to prevent android app from being Crash
我想阻止我的应用程序因未捕获的异常而崩溃。我希望将异常存储在日志中并防止应用程序崩溃。我如何实现这一目标?提前致谢。
您可以使用 try catch 块来捕获异常,这样可以防止您的应用程序崩溃。在 catch 块中获取预期的异常并将其记录下来以供您帮助。 但请记住,并非所有异常都可以被捕获
因为有些异常不是从 Exception 派生的 - 例如可抛出和错误。
基本上类型层次结构是:
Object
|
Throwable
/ \
Exception Error
只能抛出 Throwable 和派生的 类,所以如果你捕获 Throwable,那真的会捕获所有东西。
任何从 Exception(或 Exception 本身)派生的异常,而不是从 RuntimeException 派生的异常都算作已检查的异常 - 它们是您必须声明将抛出的异常,或者如果您调用抛出它们的东西则捕获的异常.
总而言之,Java 异常层次结构有点乱...
你可以自己处理未捕获的异常。
在您的应用程序中使用以下代码 class.
public class MyApplication extends Application {
private UncaughtExceptionHandler defaultUEH;
private Thread.UncaughtExceptionHandler _unCaughtExceptionHandler =
new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
// here I do logging of exception to a db
Log.d("MyApp", "Uncaught exception.");
// Do what you want.
// re-throw exception to O.S. if that is serious and need to be handled by o.s. Uncomment the next line that time.
//defaultUEH.uncaughtException(thread, ex);
}
};
public MyApplication() {
defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
// setup Thread.setDefaultUncaughtExceptionHandler(_unCaughtExceptionHandler);
}
}
为此,您只需要两个 classes 只需复制粘贴即可。
当您的应用程序中发生任何异常时,将调用此 class。
public class CrashHandler implements
java.lang.Thread.UncaughtExceptionHandler {
private final Context myContext;
private final String LINE_SEPARATOR = "\n";
public CrashHandler(Context context) {
myContext = context;
}
public void uncaughtException(Thread thread, Throwable exception) {
StringWriter stackTrace = new StringWriter();
exception.printStackTrace(new PrintWriter(stackTrace));
StringBuilder errorReport = new StringBuilder();
errorReport.append("************ CAUSE OF ERROR ************\n\n");
errorReport.append(stackTrace.toString());
errorReport.append("\n************ DEVICE INFORMATION ***********\n");
errorReport.append("Brand: ");
errorReport.append(Build.BRAND);
errorReport.append(LINE_SEPARATOR);
errorReport.append("Device: ");
errorReport.append(Build.DEVICE);
errorReport.append(LINE_SEPARATOR);
errorReport.append("Model: ");
errorReport.append(Build.MODEL);
errorReport.append(LINE_SEPARATOR);
errorReport.append("Id: ");
errorReport.append(Build.ID);
errorReport.append(LINE_SEPARATOR);
errorReport.append("Product: ");
errorReport.append(Build.PRODUCT);
errorReport.append(LINE_SEPARATOR);
errorReport.append("\n************ FIRMWARE ************\n");
errorReport.append("SDK: ");
errorReport.append(Build.VERSION.SDK);
errorReport.append(LINE_SEPARATOR);
errorReport.append("Release: ");
errorReport.append(Build.VERSION.RELEASE);
errorReport.append(LINE_SEPARATOR);
errorReport.append("Incremental: ");
errorReport.append(Build.VERSION.INCREMENTAL);
errorReport.append(LINE_SEPARATOR);
Intent intent = new Intent(myContext, ExceptionDisplay.class);
intent.putExtra("error", errorReport.toString());
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
myContext.startActivity(intent);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(10);
}
}
现在制作一个显示错误的 Activity :-
public class ExceptionDisplay extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.exception_display_layout);
TextView exception_text = (TextView) findViewById(R.id.exception_text);
Button btnBack = (Button) findViewById(R.id.btnBack);
exception_text.setText(getIntent().getExtras().getString("error"));
btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
intentData();
}
});
}
@Override
public void onBackPressed() {
intentData();
}
public void intentData() {
Log.d("CDA", "onBackPressed Called");
Intent setIntent = new Intent(ExceptionDisplay.this, AppDataSourceSelection.class);
setIntent.addCategory(Intent.CATEGORY_HOME);
setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(setIntent);
}
}
现在您可以通过扩展应用程序 Class 的 class 调用此 class:
public class ErrorHandler extends Application {
@Override
public void onCreate() {
super.onCreate();
Thread.setDefaultUncaughtExceptionHandler(new CrashHandler(getApplicationContext()));
}}
我想阻止我的应用程序因未捕获的异常而崩溃。我希望将异常存储在日志中并防止应用程序崩溃。我如何实现这一目标?提前致谢。
您可以使用 try catch 块来捕获异常,这样可以防止您的应用程序崩溃。在 catch 块中获取预期的异常并将其记录下来以供您帮助。 但请记住,并非所有异常都可以被捕获 因为有些异常不是从 Exception 派生的 - 例如可抛出和错误。
基本上类型层次结构是:
Object
|
Throwable
/ \
Exception Error
只能抛出 Throwable 和派生的 类,所以如果你捕获 Throwable,那真的会捕获所有东西。
任何从 Exception(或 Exception 本身)派生的异常,而不是从 RuntimeException 派生的异常都算作已检查的异常 - 它们是您必须声明将抛出的异常,或者如果您调用抛出它们的东西则捕获的异常.
总而言之,Java 异常层次结构有点乱...
你可以自己处理未捕获的异常。 在您的应用程序中使用以下代码 class.
public class MyApplication extends Application {
private UncaughtExceptionHandler defaultUEH;
private Thread.UncaughtExceptionHandler _unCaughtExceptionHandler =
new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
// here I do logging of exception to a db
Log.d("MyApp", "Uncaught exception.");
// Do what you want.
// re-throw exception to O.S. if that is serious and need to be handled by o.s. Uncomment the next line that time.
//defaultUEH.uncaughtException(thread, ex);
}
};
public MyApplication() {
defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
// setup Thread.setDefaultUncaughtExceptionHandler(_unCaughtExceptionHandler);
}
}
为此,您只需要两个 classes 只需复制粘贴即可。 当您的应用程序中发生任何异常时,将调用此 class。
public class CrashHandler implements
java.lang.Thread.UncaughtExceptionHandler {
private final Context myContext;
private final String LINE_SEPARATOR = "\n";
public CrashHandler(Context context) {
myContext = context;
}
public void uncaughtException(Thread thread, Throwable exception) {
StringWriter stackTrace = new StringWriter();
exception.printStackTrace(new PrintWriter(stackTrace));
StringBuilder errorReport = new StringBuilder();
errorReport.append("************ CAUSE OF ERROR ************\n\n");
errorReport.append(stackTrace.toString());
errorReport.append("\n************ DEVICE INFORMATION ***********\n");
errorReport.append("Brand: ");
errorReport.append(Build.BRAND);
errorReport.append(LINE_SEPARATOR);
errorReport.append("Device: ");
errorReport.append(Build.DEVICE);
errorReport.append(LINE_SEPARATOR);
errorReport.append("Model: ");
errorReport.append(Build.MODEL);
errorReport.append(LINE_SEPARATOR);
errorReport.append("Id: ");
errorReport.append(Build.ID);
errorReport.append(LINE_SEPARATOR);
errorReport.append("Product: ");
errorReport.append(Build.PRODUCT);
errorReport.append(LINE_SEPARATOR);
errorReport.append("\n************ FIRMWARE ************\n");
errorReport.append("SDK: ");
errorReport.append(Build.VERSION.SDK);
errorReport.append(LINE_SEPARATOR);
errorReport.append("Release: ");
errorReport.append(Build.VERSION.RELEASE);
errorReport.append(LINE_SEPARATOR);
errorReport.append("Incremental: ");
errorReport.append(Build.VERSION.INCREMENTAL);
errorReport.append(LINE_SEPARATOR);
Intent intent = new Intent(myContext, ExceptionDisplay.class);
intent.putExtra("error", errorReport.toString());
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
myContext.startActivity(intent);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(10);
}
}
现在制作一个显示错误的 Activity :-
public class ExceptionDisplay extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.exception_display_layout);
TextView exception_text = (TextView) findViewById(R.id.exception_text);
Button btnBack = (Button) findViewById(R.id.btnBack);
exception_text.setText(getIntent().getExtras().getString("error"));
btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
intentData();
}
});
}
@Override
public void onBackPressed() {
intentData();
}
public void intentData() {
Log.d("CDA", "onBackPressed Called");
Intent setIntent = new Intent(ExceptionDisplay.this, AppDataSourceSelection.class);
setIntent.addCategory(Intent.CATEGORY_HOME);
setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(setIntent);
}
}
现在您可以通过扩展应用程序 Class 的 class 调用此 class:
public class ErrorHandler extends Application {
@Override
public void onCreate() {
super.onCreate();
Thread.setDefaultUncaughtExceptionHandler(new CrashHandler(getApplicationContext()));
}}