没有强制关闭的Android异常

没有强制关闭的Android异常

本文介绍了没有强制关闭的Android异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能会丢失一些东西,但是当Android(或java)中抛出异常时,我的应用程序总是强制关闭,整个程序被终止。但是当数据库查询出现问题时,我只想返回主菜单。

  try {
database.query(params);

} catch(异常e){
Log.e(Game,Failed Loading Level,e);
returnToMenu();
}

}



这个例子强制关闭我的程序,我只想让它继续下去!

解决方案

所有的android开发人员都必须面对强制关闭问题开发应用程序。
这是一个方法来捕捉这个错误,并优雅地对待它。



这将在你的Android应用程序中创建一个错误页面的机制。被坠毁的用户将无法看到令人烦恼的弹出对话框。而不是那个应用程序将向用户显示一个预定义的视图。



为了使这样一种机制,我们需要制作一个错误处理程序和一个可以获取视图的Activity类每当应用程序被强制关闭时。

  import java.io. *; 

import android.content。*;
import android.os.Process;

public class ExceptionHandler实现java.lang.Thread.UncaughtExceptionHandler {
private final Context myContext;

public UncaughtExceptionHandler(Context context){
myContext = context;
}

public void uncaughtException(Thread thread,Throwable exception){
StringWriter stackTrace = new StringWriter();
exception.printStackTrace(new PrintWriter(stackTrace));
System.err.println(stackTrace);

Intent intent = new Intent(myContext,CrashActivity.class);
intent.putExtra(BugReportActivity.STACKTRACE,stackTrace.toString());
myContext.startActivity(intent);

Process.killProcess(Process.myPid());
System.exit(10);
}}

上面的类将作为强制关闭错误的侦听器工作。您可以看到,每当应用程序崩溃时,Intent和startActivity用于启动新的活动。所以当应用程序崩溃时,它将启动名为CrashActivity的活动。现在我已经通过了堆栈跟踪作为意图的附加功能。



现在因为CrashActivity是一个稳定的Android Actitvity你可以以任何你想要的方式处理它。



现在是重要的部分,即如何捕获该异常。
虽然很简单在您的overriden onCreate方法调用超级方法后,在每个Activity中复制以下代码行。



Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler(this)) ;



您的活动可能看起来像这样...

  public class ForceClose extends Activity {

@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler(this));

setContentView(R.layout.main);

//您的机制已经准备好了。在这个活动中,从任何地方,如果你得到强制关闭错误,它将被重定向到CrashActivity。
}}

您可以从此链接下载zip文件




I might be missing something, but whenever an Exception gets thrown in Android (or java), my application ALWAYS force closes, and the whole program is terminated. However when something goes wrong in i.e. a database query, I just want to return to the Main menu.

try {
        database.query(params);

} catch (Exception e) {
        Log.e("Game", "Failed Loading Level", e);
        returnToMenu();
    }

}

This for example force closes my program, I just want it to continue!

解决方案

All android developer must have faced force close issue while developing an application.Here is a method to catch that error and treat it elegantly.

This will create an error page kind of mechanism in your android application.So whenever your application is crashed user will not able to see that irritating pop up dialog. Instead of that app will display a predifned view to the user.

To make such kind of mechanism we need to make one error handler and an Activity class which will gain the view whenever the app gets forced closed.

import java.io.*;

import android.content.*;
import android.os.Process;

public class ExceptionHandler implements java.lang.Thread.UncaughtExceptionHandler {
    private final Context myContext;

    public UncaughtExceptionHandler(Context context) {
        myContext = context;
    }

    public void uncaughtException(Thread thread, Throwable exception) {
        StringWriter stackTrace = new StringWriter();
        exception.printStackTrace(new PrintWriter(stackTrace));
        System.err.println(stackTrace);

        Intent intent = new Intent(myContext, CrashActivity.class);
        intent.putExtra(BugReportActivity.STACKTRACE, stackTrace.toString());
        myContext.startActivity(intent);

        Process.killProcess(Process.myPid());
        System.exit(10);
    }}

Above class will work as a listener for forced close error. You can see that Intent and startActivity is used to start the new Activity whenever app crashes. So it will start the activity named CrashActivity whenever app get crashed.For now I have passed the stack trace as an intent’s extras.

Now because CrashActivity is a regualr Android Actitvity you can handle it in whatever way you want.

Now comes the important part i.e. How to catch that exception.Though it is very simple. Copy following line of code in your each Activity just after the call of super method in your overriden onCreate method.

Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler(this));

Your Activity may look something like this…

public class ForceClose extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler(this));

    setContentView(R.layout.main);

    // Your mechanism is ready now.. In this activity from anywhere if you get force close error it will be redirected to the CrashActivity.
}}

you can download zip file from this link

http://trivedihardik.wordpress.com/2011/08/20/how-to-avoid-force-close-error-in-android/

这篇关于没有强制关闭的Android异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:03