2013-03-09 71 views
-2

我只是無法在我的代碼
弄清楚這個問題我想與Facebook登錄我的應用程序
我跟了來自YouTube的視頻教程。
在代碼中,我可以使用Facebook登錄,但我無法實現註銷功能。 我試過了。 (對於註銷的代碼也有,但它仍然無法正常工作) 的代碼如下:錯誤使用Android的Facebook的SDK

後,我就登錄按鈕點擊:

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; 
     } 

哪裏是我的錯誤?

回答

0

只是你和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 
    } 
+0

更換MainActivity.this我想也這樣做。但它不工作! – user2056245 2013-03-09 05:13:15

+0

嘗試將你的休息放在最後一個括號之外,也許它正在執行,但沒有中斷,因爲它退出然後在????? (fb.isSessionValid()) – JRowan 2013-03-09 05:18:21

+0

idk它看起來對我來說很好 – JRowan 2013-03-09 05:19:31