2012-12-28 77 views
1

我需要連接到Facebook,因此我在使用網絡時使用了Tread。但我有一個問題:無法使用網絡線程連接到Facebook

Thread t = new Thread(new Runnable() { 
     @Override 
     public void run() { 
        FacebookConnectTask task = new FacebookConnectTask("facebookId", "token", "email", facebookGender,0, 0); 

        task.setOnPreExecuteListener(this); 
        task.setOnDoneListener(this); 

        task.execute(); 
       } 
    }); 
    t.start(); 

} 

我不能做

task.setOnPreExecuteListener(this); 
task.setOnDoneListener(this); 

日食給我這個錯誤:「中的任務類型的方法setOnDoneListener(Task.OnDoneListener)不適用於參數( new Runnable(){})「

我該如何解決這個問題?

謝謝!

+0

什麼是_setOnDoneListener_簽名? – sinisha

回答

1

你已經改變了上下文的一個線程你在一個惱人的類,this現在是你的自治類,而不是外部類。

試一下這個(雙關語意):

task.setOnPreExecuteListener(YourOuterClass.this); 
task.setOnDoneListener(YourOuterClass.this); 

public class YourClass implements OnDoneListener { 

    public doFacebook(){ 

     new Thread(new Runnable(){ 
     @Override 
     public void run(){ 
       task.setOnDoneListener(YourClass.this); 
     } 
     }.start(); 

    } 


    @Override 
    public void onDone(){ 

    } 

} 

或可選擇地拉你的線程類了:

public class DoSomething implements Runnable { 

     private final OnDoneListener listener; 

     public DoSomething(OnDoneListener listener){ 
      this.listener = listener; 
     } 

     @Override 
     public void run(){ 
      FacebookConnectTask task = ... ; 

      task.setOnDoneListener(listener); 
     } 


} 

public class YourActivity extends Activity implements OnDoneListener { 

    public void onCreate(Bundle b){ 
      new Thread(new DoSomething(this)).start(); 
    } 

    @Override 
    public void onDone(){ 
     // Tada 
    } 

} 

,如果你想成爲冷卻器的又一步驟創建你自己的界面,並保持所有Facebook的東西在可運行的類:

public class DoSomething implements Runnable, OnDoneListener { 

     public interface OnFacebookFinished { 
       void onFacebookFinished(); 
     } 

     private final OnFacebookFinished listener; 

     public DoSomething(OnFacebookFinished listener){ 
      this.listener = listener; 
     } 

     @Override 
     public void run(){ 
      FacebookConnectTask task = ... ; 

      task.setOnDoneListener(this); 
     } 

     @Override 
     public void onDone(){ 
      if(listener != null){ 
       listener.onFacebookFinished(); 
      } 
     } 


} 

public class YourActivity extends Activity implements OnFacebookFinished { 

    @Override 
    public void onCreate(Bundle b){ 
      new Thread(new DoSomething(this)).start(); 
    } 

    @Override 
    public void onFacebookFinished(){ 
     // Tada 
    } 

} 
+0

@roiberg你去哪了? :-) – Blundell

相關問題