2017-10-05 82 views
0

我在kotlin中使用Facebook Sdk。當我嘗試使用FB共享監聽器AccessToken.getCurrentAccessToken()時,返回null。我試過FacebookSdk.sdkInitialize(this),但已經棄用了。我怎樣才能解決這個問題AccessToken.getCurrentAccessToken()總是在Kotlin中返回null

fun SetFacebookShareListener(sd: ShareDialog) { 

      sd.registerCallback(mCallbackManagerFacebook, object : FacebookCallback<Sharer.Result> { 
       override fun onSuccess(result: Sharer.Result) { 
        // Toast.makeText(DetailsFragment.this.getActivity(), "You shared this post", Toast.LENGTH_SHORT).show(); 
        val builder1 = android.app.AlertDialog.Builder([email protected]) 
        val activeSession = AccessToken.getCurrentAccessToken() 
        if (activeSession == null) { 
         builder1.setMessage("Do you want us to Connect to your account with facebook?") 
        } else if (!activeSession.permissions.contains("publish_actions")) { 
         builder1.setMessage("Do you want us to automatically post your reward details at your facebook timeline?") 
        } 

        builder1.setPositiveButton("Yes") { dialog, which -> 
         // TODO Auto-generated method stub 

         [email protected]() 
        } 

        builder1.setNegativeButton("No") { dialog, which -> 
         // TODO Auto-generated method stub 
         // Toast.makeText(getApplicationContext(), "test1", Toast.LENGTH_LONG).show(); 
        } 

        val alert = builder1.create() 
        alert.show() 
       } 

       override fun onCancel() { 
        Toast.makeText(applicationContext, "test2", Toast.LENGTH_LONG).show() 
       } 

       override fun onError(e: FacebookException) { 
        // Toast.makeText(getApplicationContext(), "test3", Toast.LENGTH_LONG).show(); 
        e.printStackTrace() 
       } 
      }) 
     } 

,並與Facebook Connect是

fun ConnectWithFacebook() { 


    LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "email", "user_birthday")) 

} 
+0

爲了幫助解決問題,當您在應用程序的onCreate()方法中嘗試「FacebookSdk.sdkInitialize(this)」時會發生什麼?這可能是由於Facebook SDK在應用程序啓動時未正確初始化。 – Brian

+0

FacebookSdk.sdkInitialize(this)在新sdk中已棄用。任何方式我也試試這個。但對我來說沒有運氣。 –

+0

這是在Facebook登錄成功後發生的嗎?你介意發佈你的ConnectWithFacebook()的代碼嗎? – Brian

回答

0

是由LoginManager返回將被保存在共享偏好的訪問令牌,因此應用程序被打開,下一次, AccessToken.getCurrentAccessToken()應該具有相同的訪問令牌。您可以查看SDK提供的示例以查看它們的工作方式。