2015-10-07 106 views
1

我想添加一個Facebook登錄到我的應用程序。不幸的是我得到一個錯誤。我在我的strings.xml中定義了應用程序ID,並將元數據添加到我的清單中。儘管我添加了Facebook活動和使用Internet的權限,但錯誤仍然存​​在。我認爲佈局存在問題,但我看不到其中一個。也許你可以幫助我。安卓Facebook登錄 - 錯誤導致null

編輯:我改變了初始化和內容視圖功能的位置,但現在我得到一個新的錯誤。我查了一下,發現它通常是應用程序ID的問題,但我在清單中使用元數據實現了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> 

清單如下:

<?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> 

回答

1

你必須寫:

FacebookSdk.sdkInitialize(this.getApplicationContext()); 

前:

setContentView(R.layout.activity_main); 

Facebook Sdk Has Not Been Initilized

+0

我改變了它,但現在我得到的AsyncTask錯誤。谷歌說,appID缺失,但我已經添加了它。 – Neatoro