使用android-facebook-sdk时出错

时间:2013-03-09 05:00:10

标签: android facebook-android-sdk

我只是无法弄清楚代码中的问题 我想使用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;
      }

我的错误在哪里?

1 个答案:

答案 0 :(得分: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
    }