gpt4 book ai didi

java - Android Facebook 登录 - 由 null 引起的错误

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

我正在尝试将 Facebook 登录添加到我的应用程序。不幸的是我收到一个错误。我在我的 strings.xml 中定义了应用程序 ID,并将元数据添加到我的 list 中。虽然我添加了 facebook Activity 和使用互联网的权限,但错误仍然存​​在。我认为布局有问题,但我没有看到。也许你可以帮助我。

编辑:我更改了初始化和内容 View 函数的位置,但现在我收到了一个新错误。我查了一下,发现这通常是应用程序 ID 的问题,但我已经在我的 list 中使用元数据实现了该 ID。

错误如下:

10-07 10:06:36.249    4682-4748/de.homeproducts.ineedhelp E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #2
Process: de.homeproducts.ineedhelp, PID: 4682
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:304)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Object.hashCode()' on a null object reference
at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:746)
at java.util.concurrent.ConcurrentHashMap.containsKey(ConcurrentHashMap.java:774)
at com.facebook.internal.Utility.queryAppSettings(Utility.java:802)
at com.facebook.login.widget.LoginButton$1.doInBackground(LoginButton.java:502)
at com.facebook.login.widget.LoginButton$1.doInBackground(LoginButton.java:499)
at android.os.AsyncTask$2.call(AsyncTask.java:292)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)

这是我的 Activity 类:

public ImageView view = null;
public LoginButton loginButton = null;
public CallbackManager callbackManager = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

FacebookSdk.sdkInitialize(this.getApplicationContext());

callbackManager = CallbackManager.Factory.create();
loginButton = (LoginButton) this.findViewById(R.id.login_button);

loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {

@Override
public void onSuccess(LoginResult loginResult) {
Toast toast = Toast.makeText(getApplicationContext(), "Login erfolgreich!", Toast.LENGTH_LONG);
}

@Override
public void onCancel() {
Toast toast = Toast.makeText(getApplicationContext(), "Login abgebrochen!", Toast.LENGTH_LONG);
}

@Override
public void onError(FacebookException e) {
Toast toast = Toast.makeText(getApplicationContext(), "Login fehlgeschlagen!", Toast.LENGTH_LONG);
}
});

这是我的 XML 文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<ImageView android:id="@+id/hello_world" android:src="@drawable/icon" android:layout_width="128dp"
android:layout_height="128dp" android:layout_centerHorizontal="true"/>

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

</RelativeLayout>

list 看起来像这样:

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

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<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" />
</application>

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

</manifest>

最佳答案

你必须写:

FacebookSdk.sdkInitialize(this.getApplicationContext());

之前:

setContentView(R.layout.activity_main);

参见 Facebook Sdk Has Not Been Initilized

关于java - Android Facebook 登录 - 由 null 引起的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32986358/

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