gpt4 book ai didi

java - 无法在 android 应用程序中创建 facebook 登录找不到符号方法 getAccessToken()

转载 作者:行者123 更新时间:2023-11-30 01:14:33 26 4
gpt4 key购买 nike

我正在为我的 android 应用程序创建一个 facebook 登录。我遇到以下错误,我试图解决这些错误,但我无法解决,因为我找不到解决方案。

1) Error:(46, 46) error: cannot find symbol method getAccessToken()

2) 错误:(41, 55) 错误:不兼容的类型:> 无法转换为 FacebookCallback

我的activity_main.xml是:

 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#5b9bd5"
tools:context="com.example.ratingapp.ratingapp.MainActivity">


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/info"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textSize="18sp"
/>

<com.facebook.login.widget.LoginButton
android:id="@+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>


</android.support.constraint.ConstraintLayout>

我的 MainActivity.java 是:

  package com.example.ratingapp.ratingapp;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

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

import java.util.Arrays;
import java.util.List;

import static android.R.attr.data;

public class MainActivity extends AppCompatActivity {


private TextView info;
private LoginButton loginButton;
private CallbackManager callbackManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this.getApplicationContext());
setContentView(R.layout.activity_main);
callbackManager = CallbackManager.Factory.create();
loginButton = (LoginButton) findViewById(R.id.login_button);
List<String> permissionNeeds = Arrays.asList("user_photos", "email", "user_birthday", "public_profile");
loginButton.setReadPermissions(permissionNeeds);
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
info.setText(
"User ID: "
+ loginResult.getAccessToken().getUserId()
+ "\n" +
"Auth Token: "
+ loginResult.getAccessToken().getToken()
);
}

@Override
public void onCancel() {
info.setText("Login attempt canceled.");
}

@Override
public void onError(FacebookException exception) {
info.setText("Login attempt failed.");
}
});

}
}

请帮我解决这些错误,我在这段代码中做错了什么。

请同时检查我的 AndroidManifest.xml,以防我放错了 facebook 库。

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ratingapp.ratingapp">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
<activity android:name="com.facebook.FacebookActivity"
android:configChanges=
"keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:label="@string/app_name" />

<meta-data android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id"/>

</application>
<uses-permission android:name="android.permission.INTERNET"/>
/manifest>

最佳答案

loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() 
{
@Override
public void onSuccess (final LoginResult loginResult)
{
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback()
{
@Override
public void onCompleted (JSONObject object, GraphResponse response)
{
try
{
String id = response.getJSONObject().getString("id");
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(passwordEditText.getWindowToken(), 0);

if (connect())
{
/*ENTER*/
}
else
{
Toast.makeText(getApplicationContext(), textProblemConnecting(), Toast.LENGTH_LONG).show();
}
}
catch (JSONException e)
{
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("YOUR FIELDS");
request.setParameters(parameters);
request.executeAsync();
}

@Override
public void onCancel ()
{

}

@Override
public void onError (FacebookException e)
{

}
});

Bundle 参数 = new Bundle(); parameters.putString("fields", "first_name, email, id, last_name, birthday, locale"); request.setParameters(参数); 请求.executeAsync();

关于java - 无法在 android 应用程序中创建 facebook 登录找不到符号方法 getAccessToken(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38042389/

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