0

当单击按钮请求是侦听器的按钮时,我试图显示敬酒。我正在通过此按钮注销,并且我想在注销完成时显示 toast,因此我将 toast 放入请求侦听器的 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

}


 }

请帮助我被困在这里..谢谢

4

1 回答 1

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

    }


}
于 2012-10-02T08:31:36.540 回答