Android Facebook 登录 NullPointerException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25274471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:17:32  来源:igfitidea点击:

Facebook login NullPointerException

androideclipsefacebookfacebook-android-sdk

提问by Ojonugwa Jude Ochalifu

Am trying to implement the Facebook login for Android using the Facebook SDK (3.17.1) but i am having some issues i haven't had in the past. First of all, the login button doesn't render completely. This is what i get

我正在尝试使用 Facebook SDK (3.17.1) 为 Android 实现 Facebook 登录,但我遇到了一些我过去没有遇到过的问题。首先,登录按钮没有完全呈现。这就是我得到的

enter image description here

在此处输入图片说明

instead of this:

而不是这个:

enter image description here

在此处输入图片说明

(notice the missing Facebook logo). When I run the application, in the split second before it crashes it displays it just the way it should (with the Facebook logo).

(请注意缺少的 Facebook 徽标)。当我运行该应用程序时,在它崩溃之前的一瞬间,它以它应有的方式显示它(带有 Facebook 徽标)。

When the application crashes, i get this error message

当应用程序崩溃时,我收到此错误消息

"Unfortunately, FacebookLoginTest as stopped"

"Unfortunately, FacebookLoginTest as stopped"

and this in Logcat:

这在 Logcat 中:

08-12 22:19:08.751: E/AndroidRuntime(7731): FATAL EXCEPTION: AsyncTask #1
08-12 22:19:08.751: E/AndroidRuntime(7731): Process: com.example.facebooklogintest,    PID: 7731
08-12 22:19:08.751: E/AndroidRuntime(7731): java.lang.RuntimeException: An error   occured while executing doInBackground()
08-12 22:19:08.751: E/AndroidRuntime(7731):     at     android.os.AsyncTask.done(AsyncTask.java:300)
08-12 22:19:08.751: E/AndroidRuntime(7731):     at  java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
08-12 22:19:08.751: E/AndroidRuntime(7731):     at  java.util.concurrent.FutureTask.setException(FutureTask.java:222)
08-12 22:19:08.751: E/AndroidRuntime(7731):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
08-12 22:19:08.751: E/AndroidRuntime(7731):     at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231)
08-12 22:19:08.751: E/AndroidRuntime(7731):     at   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-12 22:19:08.751: E/AndroidRuntime(7731):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-12 22:19:08.751: E/AndroidRuntime(7731):     at  java.lang.Thread.run(Thread.java:864)
08-12 22:19:08.751: E/AndroidRuntime(7731): Caused by: java.lang.NullPointerException
08-12 22:19:08.751: E/AndroidRuntime(7731):     at   java.util.concurrent.ConcurrentHashMap.containsKey(ConcurrentHashMap.java:911)
08-12 22:19:08.751: E/AndroidRuntime(7731):     at   com.facebook.internal.Utility.queryAppSettings(Utility.java:377)
08-12 22:19:08.751: E/AndroidRuntime(7731):     at com.facebook.widget.LoginButton.doInBackground(LoginButton.java:677)
08-12 22:19:08.751: E/AndroidRuntime(7731):     at com.facebook.widget.LoginButton.doInBackground(LoginButton.java:1)
08-12 22:19:08.751: E/AndroidRuntime(7731):     at android.os.AsyncTask.call(AsyncTask.java:288)
08-12 22:19:08.751: E/AndroidRuntime(7731):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-12 22:19:08.751: E/AndroidRuntime(7731):     ... 4 more
08-12 22:19:10.753: D/Process(7731): killProcess, pid=7731
08-12 22:19:10.753: D/Process(7731):   com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException:131    java.lang.ThreadGroup.uncaughtException:693 java.lang.ThreadGroup.uncaughtException:690 

I followed the instructions for adding Facebook login to Android apps:

我按照将 Facebook 登录添加到 Android 应用程序的说明进行操作:

  1. Imported Facebook sdk
  2. Added Facebook project as libray
  3. Added Facebook project to list of references for my project
  4. Cleaned and even restarted the project.
  1. 导入的 Facebook sdk
  2. 将 Facebook 项目添加为 libray
  3. 将 Facebook 项目添加到我的项目的参考列表中
  4. 清理甚至重新启动项目。

Besides these, I haven't changed anything in my Activity and manifest. I have tried this in three different eclipse versions, all with the same error, but then i delete the Facebook login button code in the .xml file, the app lauches just fine. Not sure what else am supposed to do. Here are the MainActivity and Layout.xml codes:

除此之外,我的 Activity 和 manifest 中没有任何更改。我已经在三个不同的 eclipse 版本中尝试过这个,都出现了相同的错误,但随后我删除了 .xml 文件中的 Facebook 登录按钮代码,应用程序运行得很好。不知道还应该做什么。以下是 MainActivity 和 Layout.xml 代码:

MainActivity.java

主活动.java

package com.example.facebooklogintest;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity {

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


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

activity_main.xml

活动_main.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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.facebooklogintest.MainActivity" >



<com.facebook.widget.LoginButton
    android:id="@+id/authButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="30dp" />

 </RelativeLayout>

回答by apmartin1991

You need to change or add this in your manifest file

您需要在清单文件中更改或添加它

 <activity
        android:name="com.example.LoginActivity"
        android:label="@string/app_name" 
        android:parentActivityName="com.example.MainActivity" >


 </activity>
 <activity android:name="com.facebook.LoginActivity"
              android:theme="@android:style/Theme.Translucent.NoTitleBar"
              android:label="@string/app_name" />
    <meta-data android:name="com.facebook.sdk.ApplicationId"   android:value="@string/app_id"/>

I cannot take credit for this, all credit must go to the guy who posted the answer hereI am purely adding the code for ease for you and because I got told to by someone who previously deleted the answer.

我不能因此而受到赞扬,所有功劳都必须归功于在此处发布答案的人,我纯粹是为了方便您而添加代码,因为之前删除了答案的人告诉了我。

回答by Ashutoshg

10-10 11:27:47.932: E/AndroidRuntime(25915): FATAL EXCEPTION: AsyncTask #5
10-10 11:27:47.932: E/AndroidRuntime(25915): Process: com.example.version_2, PID: 25915
10-10 11:27:47.932: E/AndroidRuntime(25915): java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Object.hashCode()' on a null object reference
10-10 11:27:47.932: E/AndroidRuntime(25915):    at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:746)
10-10 11:27:47.932: E/AndroidRuntime(25915):    at java.util.concurrent.ConcurrentHashMap.containsKey(ConcurrentHashMap.java:774)
10-10 11:27:47.932: E/AndroidRuntime(25915):    at com.facebook.internal.Utility.queryAppSettings(Utility.java:822)
10-10 11:27:47.932: E/AndroidRuntime(25915):    at com.facebook.login.widget.LoginButton.run(LoginButton.java:489)

If you face above error in FB integration then the below solution will work. Just add Application ID in Meta-data tag in menifest file. It will work fine

如果您在 FB 集成中遇到上述错误,则以下解决方案将起作用。只需在menifest 文件的元数据标签中添加应用程序ID。它会正常工作

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