StrictMode 混乱 - 这是错误的线程怎么办?
我的问题:
12-18 17:05:03.336:DEBUG/StrictMode(2112):违反 StrictMode 策略; ~duration=2073 毫秒:android.os.StrictMode$StrictModeDiskReadViolation:policy=23violation=2
来自工厂方法
12-18 17:05:03.336: 调试/StrictMode(2112): 在 android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:299)
然后在我的代码中
12-18 17:05:03.336: 调试/严格模式(2112):位于 blah.ImageCache.getFromCache(ImageCache.java:248)
12-18 17:05:03.336: 调试/严格模式(2112):
在 blah2$LoaderThread$1.handleMessage(blah.java:63)
重要片段
类 LoaderThread 扩展了 HandlerThread {
公共处理程序mHandler;
公共LoaderThread(字符串名称){ 超级(名称); }
@覆盖 公共无效onLooperPrepared(){ mHandler = 新的处理程序(){ 公共无效handleMessage(消息msg){ 位图位 =ImageCache.getInstance().getFromCache((String)msg.obj,ImageCache.USE_DISK); } }; } }
ImageCache 类中的 getFromCache 方法调用
bitmap = BitmapFactory.decodeFile(fLoc);
似乎是在 UI 线程上运行,但对我来说没有任何意义。这不应该在后台线程上调用吗?毕竟,这是使用 HandlerThread 的目标......
LoadThread 类像这样在我的 onCreate(bundle) 中创建
LoaderThread 加载器 = new LoaderThread("imgLoader")
loader.start();
消息通过处理程序从 UI 线程传递
loader.mHandler.dispatchMessage(loader.mHandler.obtainMessage(args..));
我想知道这是否与静态的 getInstance 方法有关
公共静态同步ImageCache getInstance() {
如果(_instance==null){ _instance = 新的 ImageCache(); } 返回_实例; }
My issue:
12-18 17:05:03.336: DEBUG/StrictMode(2112): StrictMode policy violation; ~duration=2073 ms: android.os.StrictMode$StrictModeDiskReadViolation: policy=23 violation=2
From the factory method
12-18 17:05:03.336: DEBUG/StrictMode(2112): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:299)
Then in my code
12-18 17:05:03.336:
DEBUG/StrictMode(2112): at
blah.ImageCache.getFromCache(ImageCache.java:248)12-18 17:05:03.336:
DEBUG/StrictMode(2112):
at
blah2$LoaderThread$1.handleMessage(blah.java:63)
Important snip-its
class LoaderThread extends HandlerThread {
public Handler mHandler;
public LoaderThread(String name) {
super(name);
}@Override
public void onLooperPrepared(){
mHandler = new Handler(){
public void handleMessage(Message msg) {
Bitmap bit =ImageCache.getInstance().getFromCache((String)msg.obj,ImageCache.USE_DISK);
}
};
}
}
The getFromCache method in the ImageCache class calls
bitmap = BitmapFactory.decodeFile(fLoc);
Which seems to be running on the UI thread, but it makes no sense to me. Shouldn't this be getting called on a background thread? This was the goal of using HandlerThread after all...
The LoadThread class is created in my onCreate(bundle) like this
LoaderThread loader = new LoaderThread("imgLoader")
loader.start();
and messages are passed from the UI thread via the handler
loader.mHandler.dispatchMessage(loader.mHandler.obtainMessage(args..));
I'm wondering if this has to do with the getInstance method which is static
public static synchronized ImageCache getInstance() {
if (_instance==null) {
_instance = new ImageCache();
}
return _instance;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我现在感觉像个混蛋,但我在我的处理程序上调用了错误的方法...以
错误的方式
以正确的方式
所以不知何故,消息仍然通过处理程序运行,只是在 UI 线程而不是后台线程上运行。
I feel like an ass now, but I was calling the wrong method on my handler...
the wrong way
the right way
So somehow the messages were being run through the handler still, just on the UI thread instead of the background one.