2015-11-03 58 views
0

作爲標題狀態我的onCreateView不會被調用。我的目標是讓Facebook登錄工作。在當前狀態下,它只是緩衝並返回到相同的狀態。它不會更改爲「註銷」狀態。Android Studio(FacebookSDK) - onCreateView不被稱爲

MainActivity.java:

package com.example.frederik.snapsule; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

import com.facebook.CallbackManager; 
import com.facebook.FacebookCallback; 
import com.facebook.FacebookException; 
import com.facebook.FacebookSdk; 
import com.facebook.login.LoginManager; 
import com.facebook.login.LoginResult; 
import com.facebook.login.widget.LoginButton; 

public class MainActivity extends Activity { 

    LoginButton login; 
    TextView info; 
    CallbackManager callbackManager; 

    //Creating and initializing the feature(Code from Facebook install guide) 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     FacebookSdk.sdkInitialize(getApplicationContext()); //Calls for the facebook feature 
     setContentView(R.layout.content_main); 
     callbackManager = CallbackManager.Factory.create(); 
    } 

    public View onCreateView (
      LayoutInflater inflater, 
      ViewGroup container, 
      Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.content_main, container, false); 

     login = (LoginButton) view.findViewById(R.id.login_button); 
     login.setReadPermissions("user_friends"); 

     // Callback registration 
     login.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { 
      @Override 
      public void onSuccess(LoginResult loginResult) { 
       // App code 
      } 

      @Override 
      public void onCancel() { 
       // App code 
      } 

      @Override 
      public void onError(FacebookException exception) { 
       // App code 
      } 
     }); 
     return view; 
    } 
} 

我相信它應該以某種片段工作,但是這是一個區域新的給我,所以詳細的解答,請=)

謝謝!

回答

1

替換你MainActivity代碼與這一個:

package com.example.frederik.snapsule; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

import com.facebook.CallbackManager; 
import com.facebook.FacebookCallback; 
import com.facebook.FacebookException; 
import com.facebook.FacebookSdk; 
import com.facebook.login.LoginManager; 
import com.facebook.login.LoginResult; 
import com.facebook.login.widget.LoginButton; 

public class MainActivity extends Activity { 

    LoginButton login; 
    TextView info; 
    CallbackManager callbackManager; 

    //Creating and initializing the feature(Code from Facebook install guide) 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     FacebookSdk.sdkInitialize(getApplicationContext()); //Calls for the facebook feature 
     callbackManager = CallbackManager.Factory.create(); 
     setContentView(R.layout.content_main); 

     login = (LoginButton) findViewById(R.id.login_button); 
     login.setReadPermissions("user_friends"); 

     // Callback registration 
     login.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { 
      @Override 
      public void onSuccess(LoginResult loginResult) { 
       // App code 
      } 

      @Override 
      public void onCancel() { 
       // App code 
      } 

      @Override 
      public void onError(FacebookException exception) { 
       // App code 
      } 
     }); 

    } 

} 
+0

這個問題有所解決,但是,我的按鈕仍然只是登錄我,然後返回到「登錄Facebook」按鈕。我被告知它應該更改爲註銷 – KrownScripter

+0

要知道實際上正在做什麼,您需要在onSuccess()onCancel()中放置Toast。這樣你就可以看到發生了什麼。 –

+0

要做註銷,你可以使用LoginManager.getInstance()。logOut(); –

0

Activity沒有名爲onCreateView()的方法,因此框架不會調用它。如果您需要初始化用戶界面,請改用onCreate(),因爲onCreateView()用於Fragment s。

+0

我試圖讓按鈕工作作爲一個片段,但我怎麼分配呢? – KrownScripter