我有一个执行I/O的类(让我们叫它ABC)。一些像FileOutputStream.close这样的东西会让你在它们周围使用try catch块。此外,我创建了自己的可抛出对象,帮助用户和我知道发生了什么。
在this类中,我传入了创建它的活动的上下文,并使它成为具有可抛出文本的创建和警告对话框。
所以这就是我的问题,我需要在一个新的线程上运行这个类,但是仍然希望从可抛出的文本中获取信息。
举个例子,这是我的类中一个典型的catch子句。
new AlertDialog.Builder(myContext)
.setTitle("Error Message")
.setMessage(
"Error Code: #006" + "\n" + T.toString())
.setNeutralButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(
DialogInterface dialog,
int which) {
// TODO: Add Ability to Email
// Developer
}
}).show();我会做像这样的事情吗
throw new Throwable(throwable);这是在ABC类中代替警告对话框吗?然后,我会将警告对话框移动到调用Runnable接口run方法的try catch,还是异步任务的do in background?
发布于 2012-05-10 12:32:26
使用Java的Thread.UncaughtExceptionHandler保存文本/显示对话框。因此,您可以创建一个新的类来扩展Thread.UncaughtExceptionHandler,如下所示:
public class myThreadExceptionHandler implements Thread.UncaughtExceptionHandler
{
private DataTargetClass dataTarget;
public myThreadExceptionHandler(DataTargetClass c)
{
dataTarget = c;
}
public void uncaughtException(Thread t, Throwable e)
{
dataTarget.exceptionObject = e;
dataTarget.onException();
// Just substitute in whatever method your thread uses to return information.
}
}在启动线程的代码中,您将执行以下操作:
foo = new DataTargetClass();
Thread t = new Thread(myIoRunnable(foo));
t.setUncaughtExceptionHandler(new myThreadExceptionHandler(foo));
t.start();https://stackoverflow.com/questions/10527308
复制相似问题