解决Android中Can't create handle inside thread问题 时间:2012-03-13 12:09 来源:未知 收藏 复制分享
来源:互联网 发布:linux ssh修改文件内容 编辑:程序博客网 时间:2024/05/22 11:46
java] view plaincopyprint?
new Thread()
{
public void run()
{
.....
}
}.start();
new Thread()
{
public void run()
{
.....
}
}.start();
如果在上述代码中省略号处面创建一个Dialog的话会出现Can't create handle inside thread这样的错误,所以改用下述message形式的,下述是一个开发项目中具体的例子。
private void showStatusDialogThread() {
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (status != null) {
if (status.equals(SyncStatus.Success)) {
// 更新本地保存的lastSyncTime为当前时间
UpdateLocalLastSyncTime(System.currentTimeMillis());
}
showStatusDialog(status);
mHandler.removeCallbacks(mRunnable);
}
super.handleMessage(msg);
}
};
mRunnable = new Runnable() {
@Override
public void run() {
mHandler.postDelayed(this, 100);
Message message = new Message();
mHandler.sendMessage(message);
}
};
mHandler.post(mRunnable);
}
private void showStatusDialogThread() {
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (status != null) {
if (status.equals(SyncStatus.Success)) {
// 更新本地保存的lastSyncTime为当前时间
UpdateLocalLastSyncTime(System.currentTimeMillis());
}
showStatusDialog(status);
mHandler.removeCallbacks(mRunnable);
}
super.handleMessage(msg);
}
};
mRunnable = new Runnable() {
@Override
public void run() {
mHandler.postDelayed(this, 100);
Message message = new Message();
mHandler.sendMessage(message);
}
};
mHandler.post(mRunnable);
}
其中
showStatusDialog(status);
showStatusDialog(status);是调用一个显示Dialog的自定义的方法。
这种发送message的方式模型抽取出来就是下面的形式:
{
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
//执行的具体方法,例如创建Dialog,计数等
......
super.handleMessage(msg);
}
};
mRunnable = new Runnable() {
@Override
public void run() {
//设置延迟时间间隔
mHandler.postDelayed(this, 100);
Message message = new Message();
mHandler.sendMessage(message);
}
};
mHandler.post(mRunnable);
}
{
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
//执行的具体方法,例如创建Dialog,计数等
......
super.handleMessage(msg);
}
};
mRunnable = new Runnable() {
@Override
public void run() {
//设置延迟时间间隔
mHandler.postDelayed(this, 100);
Message message = new Message();
mHandler.sendMessage(message);
}
};
mHandler.post(mRunnable);
}
然后在其他某处调用
mHandler.removeCallbacks(mRunnable);
mHandler.removeCallbacks(mRunnable);
就可取消此message的发送。
其实这里发送message也是一种线程,相当于设置一个定时器,每隔一段时间执行某些代码一样。
- 解决Android中Can't create handle inside thread问题 时间:2012-03-13 12:09 来源:未知 收藏 复制分享
- 解决Android中Can't create handle inside thread问题
- 解决Android中Can't create handle inside thread问题
- Android中Can't create handle inside thread的解决办法
- Android问题集锦之五:Can't create handler inside thread that has not called Looper.prepare()错误的解决
- Can't create handler inside thread
- 解决Can't create handler inside thread that has not called Looper.prepare() inside AsyncTask
- android 开发遇到Can't create handler inside thread that has not called Looper.prepare()的问题
- Can't create handler inside thread that has not called Looper.prepare()错误的解决
- Can't create handler inside thread that has not called Looper.prepare()错误的解决
- 解决Can't create handler inside thread that has not called Looper.prepare()
- 解决异常Can't create handler inside thread that has not called Looper.prepare()方法
- 错误BUG解决:Can't create handler inside thread that has not called Looper.prepare()
- 解决Can't create handler inside thread that has not called Looper.prepare()
- 错误解决 Can't create handler inside thread that has not called Looper.prepare()
- Can't create handler inside thread that has not called Looper.prepare() 错误有关问题
- Can't create handler inside thread that has not called Looper.prepare() 错误有关问题
- Can't create handler inside thread that has not called Looper.prepare() 问题
- SQL 中exists的用法
- OCP-1Z0-新051-61题版本-57
- 用QTP调用VB6生成的DLL实例
- OCP-1Z0-新051-61题版本-58
- 图像处理-线性滤波-1 基础(相关算子、卷积算子、边缘效应)
- 解决Android中Can't create handle inside thread问题 时间:2012-03-13 12:09 来源:未知 收藏 复制分享
- matlab体验svm算法【非实现】
- Android开发中完全退出程序
- 获得或者修改eclipse android 默认debug 签名
- 对九个超级程序员的采访
- 寄存器信息都是在fastboot里面设置好的
- 说说C#的async和await
- delphi假死线程堵塞解决办法
- 结合实际看文档