-2

我只是无法弄清楚我的代码中的问题
我想为我的应用程序使用 FaceBook 登录
我遵循了 youtube 视频中的教程。
在代码中,我可以使用 facebook 登录,但我无法实现注销功能。我试过这样做。(注销的代码也在那里,但仍然不起作用)代码如下:

在我点击登录按钮后:

    public void onClick(View v) {
    // TODO Auto-generated method stub

    switch(v.getId()){
case R.id.fb_button:

    try{

         //to check if already logged in.
         //my code never enters the following block.

    if(fb.isSessionValid()){
        Toast.makeText(getBaseContext(), "loging out",     Toast.LENGTH_LONG).show();

        try {
            fb.logout(getBaseContext());
                         // update_fb_buttonimage() is a function to change the image of button
                          //if logged in: it shows logout
                          // else it shows login.

                          //but somehow its not working
            update_fb_buttonimage();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

              //following is working fine.
              // but every time i click n the button, it enters this bolok of code.
              //which is not what i want. this is tobe executed only if user is not logged in
              //even if  user is logged in , program enters this code only.
              // instead of this th above "if" block should be executed.

    else{
        Toast.makeText(getBaseContext(), "logging in", Toast.LENGTH_LONG).show();


        fb.authorize(LoginPage.this,new DialogListener(){

            @Override
            public void onComplete(Bundle values) {
                // TODO Auto-generated method stub
                update_fb_buttonimage();

            }

            @Override
            public void onFacebookError(FacebookError e) {
                // TODO Auto-generated method stub

                Toast.makeText(getBaseContext(), "You can't login to the facebook", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onError(DialogError e) {
                // TODO Auto-generated method stub
                Toast.makeText(getBaseContext(), "unIdentified Error", Toast.LENGTH_LONG).show();   
            }

            @Override
            public void onCancel() {
                // TODO Auto-generated method stub

                Toast.makeText(getBaseContext(), "onCancel", Toast.LENGTH_LONG).show();
            }} );
    }   }catch(Exception e){
        System.out.print(e.toString());
        Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show();
    }
        break;
      }

我的错误在哪里?

4

1 回答 1

0

只需将 MainActivity.this 替换为您的和 updateButtonImage()

if(fb.isSessionValid())
    {
        try {
            fb.logout(getApplicationContext());
            updateButtonImage();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //button close session
    }
    else
    {
        fb.authorize(MainActivity.this,new String[] {"publish_stream"}, new DialogListener(){

            @Override
            public void onFacebookError(FacebookError e)
            {
                Toast.makeText(MainActivity.this, "on Facebook error", Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onError(DialogError e)
            {
                Toast.makeText(MainActivity.this, "on error", Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onComplete(Bundle values)
            {
                updateButtonImage();

            }
            @Override
            public void onCancel()
            {

            }
        });
        //login in to facebook
    }
于 2013-03-09T05:10:22.693 回答