gpt4 book ai didi

java - Android Studio(FacebookSDK) - onCreateView 未被调用

转载 作者:行者123 更新时间:2023-12-01 10:58:12 24 4
gpt4 key购买 nike

正如标题所示,我的 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;
}
}

我相信它应该作为某种 fragment 工作,但这对我来说是一个新领域,所以请详细回答 =)

谢谢!

最佳答案

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

}

}

关于java - Android Studio(FacebookSDK) - onCreateView 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33496391/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com