-1
我有這個應用程序連接到Facebook。出於某種原因,我得到這個錯誤:Android Facebook更新UI
android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
這是我的代碼:
活動:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
app = (AppVariables)getApplicationContext();
context = getBaseContext();
facebook = app.getFacebook();
facebookRunner = new AsyncFacebookRunner(facebook);
facebookConnection = new FacebookConnection(this);
internetStatus = (TextView)findViewById(R.id.internetStatus);
facebookRunner.request("me", facebookConnection);
}
@Override
public void onFacebookResponse(String response) {
internetStatus.setText(response);
}
這是FacebookConnection類:
public class FacebookConnection implements RequestListener {
private FacebookConnectionListener listener;
public FacebookConnection (FacebookConnectionListener listener)
{
this.listener = listener;
}
public void updateFacebookListener(String response)
{
listener.onFacebookResponse(response);
}
@Override
public void onComplete(String response, Object state) {
updateFacebookListener(response);
}
@Override
public void onIOException(IOException e, Object state) {
// TODO Auto-generated method stub
}
@Override
public void onFileNotFoundException(FileNotFoundException e, Object state) {
// TODO Auto-generated method stub
}
@Override
public void onMalformedURLException(MalformedURLException e, Object state) {
// TODO Auto-generated method stub
}
@Override
public void onFacebookError(FacebookError e, Object state) {
// TODO Auto-generated method stub
}
}
這是我的聽衆界面:
public interface FacebookConnectionListener {
public void onFacebookResponse(String response);
}
怎麼了?
我不明白。 爲什麼我寫了一個聽衆。 當AsyncFacebookRunner完成時,它會用響應調用我的偵聽器。爲什麼它不好? – 2012-04-04 14:17:19
監聽器只是一種讓您知道流程何時完成(失敗等)的方式。但它與線程無關。看看這個教程:http://ericosgood.com/prog/facebook-android-sdk-tutorial/到最後它顯示如何做你想做的。 – 2012-04-04 14:30:18
是的,我發現,但如果我的RequestListener是在不同的Java類文件中怎麼做?它說他在範圍內沒有我的活動實例。 – 2012-04-04 14:40:26