我試圖用FB登錄創建一個Android應用程序。我也跟着從方向:適用於FB登錄的Android應用程序不幸的停止
http://code.tutsplus.com/tutorials/quick-tip-add-facebook-login-to-your-android-app--cms-23837
構建是成功的,但不幸的是應用程序停止。
我的logcat如下所示:
致命異常:的AsyncTask#2
工藝:com.example.android.logintest,PID:2434 顯示java.lang.NullPointerException:嘗試調用虛擬方法詮釋 java.lang.Object.hashCode()」上的空對象引用 在 java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:746) 在 java.util.concurrent.ConcurrentHashMap.containsKey(ConcurrentHashMap的的.java:7 74) at com.facebook.internal.Utility.queryAppSettings(Utility.java:823) at com.facebook.login.widget.LoginButton $ 1.run(LoginButton.java:489) at java.util.concurrent。 ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 在 java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:587) 在java.lang.Thread.run(Thread.java:818)
主要業務如下:
package com.example.android.logintest;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
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());
callbackManager = CallbackManager.Factory.create();
setContentView(R.layout.main_activity);
loginButton = (LoginButton) findViewById(R.id.login_button);
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 e) {
info.setText("Login attempt failed.");
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
添加com.facebook.FacebookActivity爲另一個活動[什麼是空指針異常,以及如何解決它?]可能重複(http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-我修復它) –
你有你的hashkey在logcat? –