Lel*_*ele 4 android exit looper android-asynctask
我有一个弯针的问题.我打电话looper.prepare(),做完之后一切正常.但如果我旋转设备,我会在准备工作中遇到异常.
07-12 16:40:09.760: E/activity(15809): java.lang.RuntimeException: Only one Looper may be created per thread
Run Code Online (Sandbox Code Playgroud)
我正试图退出弯针,但它没有做任何事情.
这是我的AsyncTask:
@Override
protected String doInBackground(String... args) {
try{Looper.prepare(); //here start the exception
try {
URL url = new URL(link);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
utente.measure(0, 0);
bmImg = decodeSampledBitmapFromResource(is,(int) utente.getMeasuredWidth(), utente.getMeasuredHeight(), link);
if(bmImg!=null){
try{
getCroppedBitmap();
}catch(Exception e){
System.out.println(e);
}
}
}
catch (IOException e)
{
Log.e("lele", "errore qui");
e.printStackTrace();
}
Looper.myLooper().quit(); //do nothings
}catch(Exception e){
Log.e("canta tu", " "+e);
}
Looper.myLooper().quit(); //do nothings
return null;
}
@Override
protected void onPostExecute(String args) {
//Looper.myLooper().quit(); //generathed an error, main thread can't stop looper
if(bmImg!=null){
try{
utente.setImageBitmap(bmImg);
ellisse.setVisibility(View.VISIBLE);
}catch(Exception e){
Log.e("lele",""+e);
Log.e("lele","errore probabile out of bound");
}
}
else {
Toast.makeText(getApplicationContext(), "Modifica la foto da \"profilo\"", Toast.LENGTH_LONG).show();
}
Run Code Online (Sandbox Code Playgroud)
想法?
Ami*_*val 17
有两种情况需要考虑:
(1)你想要在应用程序的整个生命中存活的looper线程,并且不要强烈引用视图(甚至不是隐含的)
引用谷歌工程师克里斯托弗泰特 - 你可以把弯针留在那里,直到你的应用程序被销毁,然后它会随之消失.你不必担心它.
"一般来说,永远不要退出()你的looper线程.这种方法主要是出于历史和测试的原因.在Real Life™中,我建议你继续在过程的生命周期中重复使用相同的looper线程,而不是创造/放弃他们."
我使用这样一个looper线程作为多用途HandlerThread,每当我想要在主线程(UI)之外运行时,就将Runnables发送给它.
(2)引用视图的looper线程
这个不符合Christopher Tate的建议,因为它会导致内存泄漏,例如,如果您旋转屏幕.
(你最好使处理程序线程静态并使用弱引用 - 你将返回选项#1)
要杀死它,你必须退出循环.为此,您需要在该线程的上下文上运行quit命令.
所以用msg.what创建一个带有一些int的消息,并在handleMessage中等待这个int,当它到达时 - 调用:
Looper myLooper = Looper.myLooper();
if (myLooper!=null) {
myLooper.quit();
}
Run Code Online (Sandbox Code Playgroud)
并且不要忘记取消对视图和活动的所有引用.
从您的活动中将此kill消息发送给处理程序 onDestroy()