Android Studio(FacebookSDK) - 未调用onCreateView

时间:2015-11-03 10:11:21

标签: java android

如标题所示,我的onCreateView不会被调用。我的目标是让Facebook登录工作。在当前状态下,它只是缓冲并返回到相同的状态。它不会更改为“注销”状态。

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

我认为它应该在某种形式下作为片段起作用,但这对我来说是一个新的领域,所以请详细解答=)

谢谢!

2 个答案:

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

    }

}

答案 1 :(得分:0)

Activity没有名为onCreateView()的方法,因此框架不会调用它。如果您需要初始化用户界面,请改用onCreate(),因为onCreateView()适用于Fragment