gpt4 book ai didi

android - 任何示例显示如何使用自己的按钮或 Facebook 按钮在 Android 中使用 Facebook SDK 4.0 登录?

转载 作者:IT老高 更新时间:2023-10-28 22:10:54 25 4
gpt4 key购买 nike

是否有任何教程或示例显示如何在 Android 中使用 Facebook SDK 4.0 使用自己的按钮登录?我没有到达任何地方并且使用 facebook 开发人员网站很难理解。如下所示调用 FLogin 按钮时我想检查是否登录如果用户尚未登录或登录我想要访问获取 facebook 个人资料信息和用户喜欢的 token 。

FBlogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Facebook login Code to get profile info and user likes
}
});

我也尝试过 loginButton Facebook。

  <com.facebook.login.widget.LoginButton
android:id="@+id/login_button"
android:layout_width="244dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"

/>

但它在 xml 中显示错误:-

java.lang.NoClassDefFoundError: Could not initialize class com.facebook.login.widget.LoginButton
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:413)
at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:105)
at com.android.tools.idea.rendering.LayoutlibCallback.loadView(LayoutlibCallback.java:176)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:206)
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:131)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:739)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:711)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:372)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:369)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:326)
at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:350)
at com.android.tools.idea.rendering.RenderService$5.compute(RenderService.java:708)
at com.android.tools.idea.rendering.RenderService$5.compute(RenderService.java:697)
at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:932)
at com.android.tools.idea.rendering.RenderService.createRenderSession(RenderService.java:697)
at com.android.tools.idea.rendering.RenderService.render(RenderService.java:816)
at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.doRender(AndroidLayoutPreviewToolWindowManager.java:646)
at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.access$1700(AndroidLayoutPreviewToolWindowManager.java:82)
at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$7$1.run(AndroidLayoutPreviewToolWindowManager.java:589)
at com.intellij.openapi.progress.impl.ProgressManagerImpl$2.run(ProgressManagerImpl.java:178)
at com.intellij.openapi.progress.ProgressManager.executeProcessUnderProgress(ProgressManager.java:209)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:212)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.runProcess(ProgressManagerImpl.java:171)
at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$7.run(AndroidLayoutPreviewToolWindowManager.java:584)
at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:320)
at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:310)
at com.intellij.util.ui.update.MergingUpdateQueue$2.run(MergingUpdateQueue.java:254)
at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:269)
at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:227)
at com.intellij.util.ui.update.MergingUpdateQueue.run(MergingUpdateQueue.java:217)
at com.intellij.util.concurrency.QueueProcessor.runSafely(QueueProcessor.java:238)
at com.intellij.util.Alarm$Request$1.run(Alarm.java:327)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

我的 Activity 课:-

protected void onCreate(Bundle savedInstanceState)  {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_main);

我有任何使用 facebook sdk4.0 登录的示例/教程吗?我被两种方式都困住了。请帮忙。

最佳答案

这可能对你有帮助

// Custom button
private Button fbbutton;

// Creating Facebook CallbackManager Value
public static CallbackManager callbackmanager;

@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Initialize SDK before setContentView(Layout ID)
FacebookSdk.sdkInitialize(getApplicationContext());

setContentView(R.layout.activity_main);

// Initialize layout button
fbbutton = (Button) findViewById(R.id.button2);

fbbutton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// Call private method
onFblogin();
}
});

}

// Private method to handle Facebook login and callback
private void onFblogin()
{
callbackmanager = CallbackManager.Factory.create();

// Set permissions
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("email","user_photos","public_profile"));

LoginManager.getInstance().registerCallback(callbackmanager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {

System.out.println("Success");
GraphRequest.newMeRequest(
loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject json, GraphResponse response) {
if (response.getError() != null) {
// handle error
System.out.println("ERROR");
} else {
System.out.println("Success");
try {

String jsonresult = String.valueOf(json);
System.out.println("JSON Result"+jsonresult);

String str_email = json.getString("email");
String str_id = json.getString("id");
String str_firstname = json.getString("first_name");
String str_lastname = json.getString("last_name");

} catch (JSONException e) {
e.printStackTrace();
}
}
}

}).executeAsync();

}

@Override
public void onCancel() {
Log.d(TAG_CANCEL,"On cancel");
}

@Override
public void onError(FacebookException error) {
Log.d(TAG_ERROR,error.toString());
}
});
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

callbackmanager.onActivityResult(requestCode, resultCode, data);
}

在 list 中添加以下内容,

<application
android:allowBackup="true"
android:icon="@drawable/app_icon"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
.
.
<!-- [START Facebook] -->
<activity
android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />

<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id" />
<!-- [END Facebook] -->
.
.
</application>

关于android - 任何示例显示如何使用自己的按钮或 Facebook 按钮在 Android 中使用 Facebook SDK 4.0 登录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29333335/

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