当我点击按钮请求的监听器按钮时,我正试图显示一个祝酒词。我正在通过此按钮注销,我想在loggout完成时显示吐司,所以我在请求侦听器的onComplete方法中放入吐司。这是我的完整代码
包含按钮侦听器的HomeActivity
public class HomeActivity extends Activity implements OnClickListener{
private static final String TAG = "Facebook";
private Button mLogin, mLogout, mShare;
private Facebook facebook;
private AsyncFacebookRunner abRunner;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_home);
//mLogin.setBackgroundColor(Color.BLUE);
// Initialize facebook objects
facebook = new Facebook("479652662068145");
abRunner = new AsyncFacebookRunner(facebook);
// Setup VIews
mLogin= (Button) findViewById(R.id.Login);
mLogout= (Button) findViewById(R.id.Logout);
mLogin.setOnClickListener(this);
mLogout.setOnClickListener(this);
}
@Override
public void onClick(View v){
int id = v.getId();
switch(id){
case R.id.Login:
FacebookLoginDialog login = new FacebookLoginDialog();
facebook.authorize(this, login);
break;
case R.id.Logout:
FacebookLogoutRequest logout = new FacebookLogoutRequest(this);
abRunner.logout(this, logout);
break;
default:
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_home, menu);
return true;
}
}
和我的RequestListener代码
public class FacebookLogoutRequest implements RequestListener{
private Context context;
public FacebookLogoutRequest (Context context){
this.context= context;
}
public void onComplete(String response, Object state) {
Toast toast = Toast.makeText(context, "You Are Logged Out", Toast.LENGTH_SHORT);
toast.show();
}
@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
}
}
请帮助我被困在这里..谢谢
答案 0 :(得分:0)
使用Activity而不是Context,因为上下文不适用于UI引用
public class FacebookLogoutRequest implements RequestListener{
private Activity context;
public FacebookLogoutRequest (Activity context){
this.context= context;
}
public void onComplete(String response, Object state) {
Toast toast = Toast.makeText(context, "You Are Logged Out", Toast.LENGTH_SHORT);
toast.show();
}
@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
}
}