解决方案 »

  1.   

    android.os.Process.killProcess(android.os.Process.myPid());
    System.exit(1);
    都执行这个了,出现异常肯定是会关闭的。另外不重启就起不来嘛,你试试把android.os.Process.killProcess(android.os.Process.myPid());注释掉
      

  2.   



    1、你说的没有用
    2、我想达到的其实很简单: 我程序出现异常,捕获到异常,把异常内容保存到log文件,弹出友好的提醒客户,退出程序。后面用户在打开程序就正常运行。

    我上面提供的代码,能实现:把异常内容保存到log文件,弹出友好的提醒客户,退出程序;当用户在打开程序,还是 捕获到异常  然后程序在保存错误日志,退出。  我就希望再次点击程序 ,以前的异常情况就不要存在了,不然出现错误后,用户只能重新启动程序才可以打开程序了。
      

  3.   



    1、你说的没有用
    2、我想达到的其实很简单: 我程序出现异常,捕获到异常,把异常内容保存到log文件,弹出友好的提醒客户,退出程序。后面用户在打开程序就正常运行。

    我上面提供的代码,能实现:把异常内容保存到log文件,弹出友好的提醒客户,退出程序;当用户在打开程序,还是 捕获到异常  然后程序在保存错误日志,退出。  我就希望再次点击程序 ,以前的异常情况就不要存在了,不然出现错误后,用户只能重新启动程序才可以打开程序了。首先,你要知道是哪里出了异常,不解决这个出错的地方,你每次打开都是会这样的
      

  4.   

    初看代码逻辑为
    每当发生该异常时,就会kill当前应用 
    如果想实现不退出,可增加一个判断
    例如,判断这个异常的日志已记录,那么就不去kill 
      

  5.   

       kill  然后重新打开这个应用。
    高手帮我解决下
      

  6.   



    1、你说的没有用
    2、我想达到的其实很简单: 我程序出现异常,捕获到异常,把异常内容保存到log文件,弹出友好的提醒客户,退出程序。后面用户在打开程序就正常运行。

    我上面提供的代码,能实现:把异常内容保存到log文件,弹出友好的提醒客户,退出程序;当用户在打开程序,还是 捕获到异常  然后程序在保存错误日志,退出。  我就希望再次点击程序 ,以前的异常情况就不要存在了,不然出现错误后,用户只能重新启动程序才可以打开程序了。楼主,你实现了把异常内容保存到log文件,弹出友好的提醒客户,退出程序,但是你并没有捕获系统的异常并抛出,所以你程序的异常仍然存在。下面是我写的异常类方法:
    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
    if (!handleException(ex) && mDefaultHandler != null) {
    // 如果用户没有处理则让系统默认的异常处理器来处理
    mDefaultHandler.uncaughtException(thread, ex);
    } else {
    try {
    Thread.sleep(3000);
    } catch (InterruptedException e) {
    Log.e(TAG, "error : ", e);
    }
    // 退出程序
    android.os.Process.killProcess(android.os.Process.myPid());
    System.exit(1);
    }
    } /**
     * 自定义错误处理,收集错误信息 发送错误报告等操作均在此完成.
     * 
     * @param ex
     * @return true:如果处理了该异常信息;否则返回false.
     */
    private boolean handleException(Throwable ex) {
    if (ex == null) {
    return false;
    }
    // 使用Toast来显示异常信息
    new Thread() {
    @Override
    public void run() {
    Looper.prepare();
     Toast.makeText(mContext, "很抱歉,程序出现异常,即将退出.", Toast.LENGTH_LONG).show();
    Looper.loop();
    }
    }.start(); if (createFile) {
    // 保存日志文件
    saveCrashInfo2File(ex);
    } return true;
    }
      

  7.   

    当前应用被你kill掉了,是没法再去执行其它的方法
    建议你是不是可以写个服务去监听你这个应用的进程,如果被kill掉了,那么再用startActivity去启动它?
      

  8.   

    代码改成:package comm.funclass;import java.io.File;
    import java.io.FileOutputStream;
    import java.io.PrintWriter;
    import java.io.StringWriter;
    import java.io.Writer;
    import java.lang.Thread.UncaughtExceptionHandler;
    import java.util.HashMap;
    import java.util.Map;import comm.funclass.FunClass.PhoneInfo;import ui.main.ErrorActivity;import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Process;
    import android.util.Log;public class ExceptionHanlder implements UncaughtExceptionHandler { private Context mContext = null;
    private Thread.UncaughtExceptionHandler mDefaultHandler;
    private static ExceptionHanlder instance;
    public static final String TAG = "ExceptionHanlder";
    // 用来存储设备信息和异常信息
    private Map<String, String> infos = new HashMap<String, String>(); public ExceptionHanlder() {
    } public static ExceptionHanlder getInstance() {
    if (instance == null)
    instance = new ExceptionHanlder();
    return instance;
    } public void init(Context context) {
    mContext = context;
    mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
    Thread.setDefaultUncaughtExceptionHandler(this);
    } @Override
    public void uncaughtException(Thread thread, Throwable ex) {
    if (!handleException(ex) && mDefaultHandler != null) {
    // 如果用户没有处理则让系统默认的异常处理器来处理
    mDefaultHandler.uncaughtException(thread, ex);
    } else {
    try {
    Thread.sleep(3000);
    } catch (InterruptedException e) {
    Log.e(TAG, "error : ", e);
    }
    // 退出程序
    Process.killProcess(Process.myPid());
    System.exit(1);
    }
    } private boolean handleException(Throwable ex) {
    if (ex == null) {
    return false;
    }
    // 收集设备参数信息
    PhoneInfo phoneMap = GlobalApp.getInstance().phoneinfo;
    infos.put("DeviceId", phoneMap.DeviceId);
    infos.put("Phone", phoneMap.Phone); infos.put("IMEI", phoneMap.IMEI);
    infos.put("IMSI", phoneMap.IMSI);
    infos.put("IP", phoneMap.IP);
    infos.put("MAC", phoneMap.MAC);
    infos.put("SDK", phoneMap.SDK);
    infos.put("Model", phoneMap.Model);
    infos.put("Release", phoneMap.Release);
    // 保存日志文件
    String sError = saveCatchInfo2File(ex); Intent intent = new Intent(mContext, ErrorActivity.class);
    intent.putExtra("Error", sError);
    mContext.startActivity(intent);
    crashClose(mContext);
    return true;
    } private String saveCatchInfo2File(Throwable ex) {
    StringBuffer sb = new StringBuffer();
    for (Map.Entry<String, String> entry : infos.entrySet()) {
    String key = entry.getKey();
    String value = entry.getValue();
    sb.append(key + "=" + value + "\n");
    } Writer writer = new StringWriter();
    PrintWriter printWriter = new PrintWriter(writer);
    ex.printStackTrace(printWriter);
    Throwable cause = ex.getCause();
    while (cause != null) {
    cause.printStackTrace(printWriter);
    cause = cause.getCause();
    }
    printWriter.close();
    String result = writer.toString();
    sb.append(result);
    try {
    long timestamp = System.currentTimeMillis();
    String time = FunClass.getDateFarmat(FunClass.dDataFormat);
    String fileName = time + "-" + timestamp + ".log"; String Path = GlobalApp.MobileSdcardDataDir + "/crash/";
    File fileParent = new File(Path);
    if (!fileParent.exists()) {
    fileParent.mkdirs();
    }
    FileOutputStream fos = new FileOutputStream(Path + fileName);
    fos.write(sb.toString().getBytes());
    fos.close();
    return fileName;
    } catch (Exception e) {
    Log.e(TAG, "an error occured while writing file...", e);
    }
    return "";
    } private boolean crashClose(Context context) {
    if (context == null) {
    return false;
    } if (context instanceof Activity) {
    ((Activity) context).finish();
    } Process.killProcess(Process.myPid());
    System.exit(1);
    return true;
    }
    }
    基本上可以用了,先保存错误日志,然后弹出错误处理页面。当还有一个小问题  出错后老弹出系统提醒:很抱歉,XXX 已停止运行。   这个提醒我想把弄掉,不要提醒了。
      

  9.   

    我测试,有的异常 也不弹出:很抱歉,XXX 已停止运行   当有的异常 弹出:很抱歉,XXX 已停止运行那个高手告诉我怎么全部不要弹出这个