我为程序写了一个异常处理类,有下面问题 异常处理exception异常 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 android.os.Process.killProcess(android.os.Process.myPid());System.exit(1);都执行这个了,出现异常肯定是会关闭的。另外不重启就起不来嘛,你试试把android.os.Process.killProcess(android.os.Process.myPid());注释掉 1、你说的没有用2、我想达到的其实很简单: 我程序出现异常,捕获到异常,把异常内容保存到log文件,弹出友好的提醒客户,退出程序。后面用户在打开程序就正常运行。我上面提供的代码,能实现:把异常内容保存到log文件,弹出友好的提醒客户,退出程序;当用户在打开程序,还是 捕获到异常 然后程序在保存错误日志,退出。 我就希望再次点击程序 ,以前的异常情况就不要存在了,不然出现错误后,用户只能重新启动程序才可以打开程序了。 1、你说的没有用2、我想达到的其实很简单: 我程序出现异常,捕获到异常,把异常内容保存到log文件,弹出友好的提醒客户,退出程序。后面用户在打开程序就正常运行。我上面提供的代码,能实现:把异常内容保存到log文件,弹出友好的提醒客户,退出程序;当用户在打开程序,还是 捕获到异常 然后程序在保存错误日志,退出。 我就希望再次点击程序 ,以前的异常情况就不要存在了,不然出现错误后,用户只能重新启动程序才可以打开程序了。首先,你要知道是哪里出了异常,不解决这个出错的地方,你每次打开都是会这样的 初看代码逻辑为每当发生该异常时,就会kill当前应用 如果想实现不退出,可增加一个判断例如,判断这个异常的日志已记录,那么就不去kill kill 然后重新打开这个应用。高手帮我解决下 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; } 当前应用被你kill掉了,是没法再去执行其它的方法建议你是不是可以写个服务去监听你这个应用的进程,如果被kill掉了,那么再用startActivity去启动它? 代码改成: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 已停止运行。 这个提醒我想把弄掉,不要提醒了。 我测试,有的异常 也不弹出:很抱歉,XXX 已停止运行 当有的异常 弹出:很抱歉,XXX 已停止运行那个高手告诉我怎么全部不要弹出这个 httpPost中上传图片 httpclient实现 关于6个radiobutton布局的问题 关于Android本地流量统计和运营商统计结果的差异问题 android底层开发环境如何搭建? Android中string文件的问题 关于import的问题 Android2.2 触摸键点亮? listview中显示一个imageview 刚接触android,麻烦看看为什么这布局会报错! HttpGet请求--返回结果有问题? 怎么样将一个图片的隐藏与否作为条件? Android循环播放一组视频出现如下错误,求高手解决
System.exit(1);
都执行这个了,出现异常肯定是会关闭的。另外不重启就起不来嘛,你试试把android.os.Process.killProcess(android.os.Process.myPid());注释掉
1、你说的没有用
2、我想达到的其实很简单: 我程序出现异常,捕获到异常,把异常内容保存到log文件,弹出友好的提醒客户,退出程序。后面用户在打开程序就正常运行。
我上面提供的代码,能实现:把异常内容保存到log文件,弹出友好的提醒客户,退出程序;当用户在打开程序,还是 捕获到异常 然后程序在保存错误日志,退出。 我就希望再次点击程序 ,以前的异常情况就不要存在了,不然出现错误后,用户只能重新启动程序才可以打开程序了。
1、你说的没有用
2、我想达到的其实很简单: 我程序出现异常,捕获到异常,把异常内容保存到log文件,弹出友好的提醒客户,退出程序。后面用户在打开程序就正常运行。
我上面提供的代码,能实现:把异常内容保存到log文件,弹出友好的提醒客户,退出程序;当用户在打开程序,还是 捕获到异常 然后程序在保存错误日志,退出。 我就希望再次点击程序 ,以前的异常情况就不要存在了,不然出现错误后,用户只能重新启动程序才可以打开程序了。首先,你要知道是哪里出了异常,不解决这个出错的地方,你每次打开都是会这样的
每当发生该异常时,就会kill当前应用
如果想实现不退出,可增加一个判断
例如,判断这个异常的日志已记录,那么就不去kill
高手帮我解决下
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;
}
建议你是不是可以写个服务去监听你这个应用的进程,如果被kill掉了,那么再用startActivity去启动它?
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 已停止运行。 这个提醒我想把弄掉,不要提醒了。