2012-01-14 13 views
0

我無法弄清楚,爲什麼我不能設置文本到我的TextView的電視。 獲得:無法觸及另一個線程的視圖,如何使它成爲可能?

E/AndroidRuntime(686): android.view.ViewRoot$CalledFromWrongThreadException: 
Only the original thread that created a view hierarchy can touch its views. 

我嘗試過很多辦法去彌補。 正如你可以看到我試着處理程序,因爲我曾與敬酒同樣的問題。現在,敬酒的作品,但的setText犯規:(( 請幫助我的人,我應該如何配置該處理器

public class calculate extends Activity implements OnClickListener { 
    private myService myService; //bound service instance 
    private boolean serviceStarted; 
    View show_map; 
    View data; 
    View start; 
    View stop; 
    public TextView tv; 
    private Location loc; 
    private boolean initiated=false; 
    private float distance=0; 
    UIHandler uiHandler; 
    route_calc rc; 

public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.calculate); 
    tv=(TextView)findViewById(R.id.textView1); 
    show_map=findViewById(R.id.button1); 
    show_map.setOnClickListener(this); 
    data=findViewById(R.id.button2); 
    data.setOnClickListener(this); 
    start=findViewById(R.id.button3); 
    start.setOnClickListener(this); 
    stop=findViewById(R.id.button4); 
    stop.setVisibility(View.INVISIBLE); 
    stop.setOnClickListener(this); 
    HandlerThread uiThread = new HandlerThread("UIHandler"); 
    uiThread.start(); 
    uiHandler = new UIHandler(uiThread.getLooper()); 

} 

public void onDestroy(){ 
    super.onDestroy(); 
} 


@Override 
public void onClick(View v) { 
    Intent i; 
    switch(v.getId()){ 
    case R.id.button1: 
     i=new Intent(this,Map.class); 
     startActivity(i); 
     break; 
    case R.id.button2: 
     i=new Intent(this,data.class); 
     startActivity(i); 
     break; 
    case R.id.button3: 
     startService(); 

     break; 
    case R.id.button4: 
     stopService(); 
     break; 
    } 

} 

//connection between this activity and service myService 
ServiceConnection myServConn = new ServiceConnection() { 
    @Override 
    public void onServiceDisconnected(ComponentName arg0) { 
     myService = null; 
    } 
    @Override 
    public void onServiceConnected(ComponentName arg0, IBinder binder) { 
     myService = ((myService.MyBinder)binder).getMyService(); 
    } 
}; 

private void startService() { 
    Intent intent = new Intent(this, myService.class); 
    startService(intent); 
    //Bind MyService here 
    bindService(intent, myServConn, BIND_AUTO_CREATE); 
    stop.setVisibility(View.VISIBLE); 
    serviceStarted = true; 
    rc = new route_calc(); 
    rc.start(); 
} 

private void stopService() { 
    if(serviceStarted) { 
     Intent intent = new Intent(this, myService.class); 
     //Unbind MyService here 
     unbindService(myServConn); 
     stopService(intent); 
     stop.setVisibility(View.INVISIBLE); 

     serviceStarted = false; 
    } 
} 

void showToast(String s){ 
    handleUIRequest(s); 
} 

void setText(){ 
    handleUISetText(); 
} 

class route_calc extends Thread{ 
    Location begin; 
    public void run() { 
     float temp; 


     while(!initiated){ 
      try{ 

       loc=myService.getLocation(); 

      } 
      catch(Exception e){ 

      } 

      if(loc!=null){ 
       begin=loc; 
       initiated=true; 
       showToast("zadzialalo"); 
      } 

     } 
     while(true){ 
      loc=myService.getLocation(); 
      temp=begin.distanceTo(loc); 
      distance=distance+temp; 
      tv.setText("przejechales "+distance+" m"); 
      System.err.println(distance); 
      begin=loc; 
      try { 
       this.sleep(500); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 

     } 

    } 

} 


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

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


    public void handleMessage(Message msg) 
    { 
     switch(msg.what) 
     { 
     case UIHandler.DISPLAY_UI_TOAST: 
     { 
      Context context = getApplicationContext(); 
      Toast t = Toast.makeText(context, (String)msg.obj, Toast.LENGTH_LONG); 
      t.show(); 
     } 

     case UIHandler.TV_SET_TEXT: 
     { 

      tv.setText("przejechałeś "+distance+" m"); 
     } 
     default: 
      break; 
     } 
    } 
} 

protected void handleUIRequest(String message) 
{ 
    Message msg = uiHandler.obtainMessage(UIHandler.DISPLAY_UI_TOAST); 
    msg.obj = message; 
    uiHandler.sendMessage(msg); 
} 

protected void handleUISetText(){ 
    Message msg=uiHandler.obtainMessage(UIHandler.TV_SET_TEXT); 
    uiHandler.sendMessage(msg); 
} 

} 

回答

0

好像你把你的整個活動在這裏,而且還包括服務,你沒? 「T嘗試縮小的問題。

在route_calc線程調用showToast,這可能是你的問題,你應該從你的處理程序調用showToast(或任何其他UI功能)。

喜歡的東西這樣的:

您是否想在你的線程東西:

new Thread(new Runnable() 
      { 
       @Override 
       public void run() 
       { 
        try 
        { 
         someHeavyStuffHere(); //Big calculations or file download here. 
         handler.sendEmptyMessage(SUCCESS); 
        } 
        catch (Exception e) 
        { 
         handler.sendEmptyMessage(FAILURE); 
        } 
       } 
      }).start(); 

當你的數據準備好,告訴處理程序把它放在一個視圖,並顯示它:

protected Handler handler = new Handler() 
    { 
     @Override 
     public void handleMessage(Message msg) 
     { 
      if (msg.what == SUCCESS) 
      { 
       setCalculatedDataToaView(); // the data you calculated from your thread can now be shown in one of your views. 
      } 
      else if (msg.what == FAILURE) 
      { 
       errorHandlerHere();//could be your toasts or any other error handling... 
      } 
     } 
    }; 
+1

非常感謝你。這是非常有用的:)) 你解決了我的整個問題。 – kolakao 2012-01-15 13:43:57

+0

太棒了,玩得開心:) – Rotemmiz 2012-01-15 14:25:59

相關問題