2013-08-25 149 views
0

我需要從外部更新我的UI Thread。我無法使用runOnUiThread,因爲它是使用StandOut庫的應用程序。通過處理程序更新UI

所以我在含方法來更新UI的類創建了Handler

private final class UIHandler extends Handler { 
    public static final int DISPLAY_UI_TOAST = 0; 
    private static final int LOAD_PROFILE = 1; 

    public UIHandler(Looper looper) { 
     super(looper); 
    } 

    @Override 
    public void handleMessage(Message msg) { 
     switch (msg.what) { 
     case UIHandler.DISPLAY_UI_TOAST: { 
      Context context = mw.getApplicationContext(); 
      Toast t = Toast.makeText(context, (String) msg.obj, 
        Toast.LENGTH_SHORT); 
      t.show(); 
     } 
     case UIHandler.LOAD_PROFILE:{ 
      loadProfile((String) msg.obj); 
     } 
     default: 
      break; 
     } 
    } 
} 

我然後我創建的消息發送到Handler

public void loadP(String prof){ 
    Message msg = uiHandler.obtainMessage(UIHandler.LOAD_PROFILE); 
    msg.obj = prof; 
    uiHandler.sendMessage(msg); 
} 

的方法但是,當我打電話loadP(myProfile),該應用程序與此錯誤崩潰:

08-25 19:55:08.428: E/AndroidRuntime(8602): FATAL EXCEPTION: UIHandler 
08-25 19:55:08.428: E/AndroidRuntime(8602): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 
08-25 19:55:08.428: E/AndroidRuntime(8602):  at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4607) 
08-25 19:55:08.428: E/AndroidRuntime(8602):  at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:835) 
08-25 19:55:08.428: E/AndroidRuntime(8602):  at android.view.View.requestLayout(View.java:15129) 
08-25 19:55:08.428: E/AndroidRuntime(8602):  at android.view.View.requestLayout(View.java:15129) 
08-25 19:55:08.428: E/AndroidRuntime(8602):  at android.view.View.requestLayout(View.java:15129) 
08-25 19:55:08.428: E/AndroidRuntime(8602):  at android.view.ViewGroup.addView(ViewGroup.java:3249) 
08-25 19:55:08.428: E/AndroidRuntime(8602):  at android.view.ViewGroup.addView(ViewGroup.java:3196) 
08-25 19:55:08.428: E/AndroidRuntime(8602):  at android.view.ViewGroup.addView(ViewGroup.java:3172) 
08-25 19:55:08.428: E/AndroidRuntime(8602):  at com.vektor.amapper.elements.KeyElement.createButton(KeyElement.java:261) 
08-25 19:55:08.428: E/AndroidRuntime(8602):  at com.vektor.amapper.elements.KeyElement.setButton(KeyElement.java:159) 
08-25 19:55:08.428: E/AndroidRuntime(8602):  at com.vektor.amapper.elements.KeyElement.<init>(KeyElement.java:81) 
08-25 19:55:08.428: E/AndroidRuntime(8602):  at com.vektor.amapper.util.KeyElementManager.add(KeyElementManager.java:32) 
08-25 19:55:08.428: E/AndroidRuntime(8602):  at com.vektor.amapper.util.KeyElementManager.loadProfileUI(KeyElementManager.java:91) 
08-25 19:55:08.428: E/AndroidRuntime(8602):  at com.vektor.amapper.util.MapperController.loadProfileUI(MapperController.java:106) 
08-25 19:55:08.428: E/AndroidRuntime(8602):  at com.vektor.amapper.windows.ui.ProfilesPanel.loadProfile(ProfilesPanel.java:423) 
08-25 19:55:08.428: E/AndroidRuntime(8602):  at com.vektor.amapper.windows.ui.ProfilesPanel.access$1(ProfilesPanel.java:414) 
08-25 19:55:08.428: E/AndroidRuntime(8602):  at com.vektor.amapper.windows.ui.ProfilesPanel$UIHandler.handleMessage(ProfilesPanel.java:464) 
08-25 19:55:08.428: E/AndroidRuntime(8602):  at android.os.Handler.dispatchMessage(Handler.java:99) 
08-25 19:55:08.428: E/AndroidRuntime(8602):  at android.os.Looper.loop(Looper.java:137) 
08-25 19:55:08.428: E/AndroidRuntime(8602):  at android.os.HandlerThread.run(HandlerThread.java:60) 

I想要添加DISPLAY_UI_TOAST消息正在工作。

+0

只有UI線程可以更新觸摸意見。 –

回答

1

你不必一直延伸Handler類,如果你只是想處理一些郵件,只需要實現Handler.Callback

private final class UICallback implements Handler.Callback{ 
    public static final int DISPLAY_UI_TOAST = 0; 
    private static final int LOAD_PROFILE = 1; 

    @Override 
    public boolean handleMessage(Message msg) { 
     switch (msg.what) { 
      case UICallback.DISPLAY_UI_TOAST: { 
       Context context = mw.getApplicationContext(); 
       Toast t = Toast.makeText(context, (String) msg.obj, 
         Toast.LENGTH_SHORT); 
       t.show(); 
       return true; 
      } 
      case UICallback.LOAD_PROFILE:{ 
       loadProfile((String) msg.obj); 
       return true; 
      } 
      default: 
       return false; 
     } 
    } 
} 

注意,在上面的代碼,返回true被處理消息時,假的時候不是給消息我們。

現在,創建一個處理程序與主尺蠖(讓所有的消息在UI線程中運行),以及我們所創建的回調:

Handler handler = new Handler(Looper.getMainLooper(),new UICallback()); 
+0

使用主循環完美地工作! – Vektor88