其中,"退出"弯针? [英] where "quit" the looper?

查看:129
本文介绍了其中,"退出"弯针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

i'have有问题的活套... 我叫尺蠖。prepare(),并做出头,经过这一切工作正常。 但是,如果我旋转设备,我对prepare异常

i'have a problem with a looper... I call looper.prepare(), and after do somethings, and it all works fine. But if i rotate the device, i have an exception on the prepare

07-12 16:40:09.760: E/activity(15809):  java.lang.RuntimeException: Only one Looper may be created per thread

我想退出尺蠖,但它does'n无能为​​力......

I'm trying to quitting the looper, but it does'n do nothing...

还有我的异步任务

 @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();
       }

想法?

推荐答案

有两种情况考虑:

您想要的(1)环线生活应用程序的整个生命周期,并且不持有强引用视图(甚至没有暗示)

报价谷歌工程师,克里斯托弗·泰特 - 你可以离开弯针,直到您的应用程序被破坏,它会下跌。你不必为此担心。

Quoting Google engineer, Christopher Tate - you can just leave the looper there until your app is destroyed, and it will go down with it. You don't need to worry about it.

说到很一般,从没有放弃过()的环线。这个方法的存在主要是历史和测试的原因。在现实生活中™,我建议你继续重复使​​用相同的弯针线(秒)的生活过程,而不是创建/退出他们。

"Speaking very generally, never quit() your looper threads. That method exists mostly for historical and testing reasons. In Real Life™, I recommend that you continue to reuse the same looper thread(s) for the life of the process rather than creating/quitting them."

我用这样的弯针线为多用途 HandlerThread 和发送的Runnable它每当我想要的东西的主线程(UI)之外运行。

I use such a looper thread as a multi purpose HandlerThread, and send Runnables to it whenever I want something to run outside the main thread (UI).

(2)具有参照视图环线

这此起彼伏了克里斯托弗·泰特的建议,因为它会导致内存泄漏,例如,如果您旋转屏幕。
(你最好让处理线程的静态和使用弱引用 - 你会回来与选项#1)
要杀死它,你必须退出循环。要做到这一点,你需要运行quit命令在该线程的上下文。
因此,创建一个消息,一些无论INT为您msg.what,并在您的handleMessage等待这个中断,当它到达 - 呼叫:

This one falls out of the recommendation of Christopher Tate, because it will cause memory leak, for example if you rotate the screen.
(You better make the handler thread static and use weak reference - and you'll be back with option #1)
To kill it you must quit the loop. To do that, you need to run the quit command on the context of that thread.
So create a message with some whatever int as your msg.what, and in your handleMessage wait for this int, and when it arrives - call:

Looper myLooper = Looper.myLooper();
if (myLooper!=null) {
    myLooper.quit();
}

和别忘了NULL的参考意见和活动。

And don't forget to null all reference to views and activities.

发送这个杀消息处理程序,从你的活动的onDestroy()

Send this kill message to the handler from your activity onDestroy()

这篇关于其中,"退出"弯针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆