我正在尝试与Facebook集成时遇到以下错误:
11-18 15:57:54.186 23052-23093/com.creaa.admin.mylogin E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
11-18 15:57:54.186 23052-23093/com.creaa.admin.mylogin E/AndroidRuntime: Process: com.creaa.admin.mylogin, PID: 23052
11-18 15:57:54.186 23052-23093/com.creaa.admin.mylogin E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Object.hashCode()' on a null object reference
11-18 15:57:54.186 23052-23093/com.creaa.admin.mylogin E/AndroidRuntime: at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:746)
11-18 15:57:54.186 23052-23093/com.creaa.admin.mylogin E/AndroidRuntime: at java.util.concurrent.ConcurrentHashMap.containsKey(ConcurrentHashMap.java:774)
11-18 15:57:54.186 23052-23093/com.creaa.admin.mylogin E/AndroidRuntime: at com.facebook.internal.Utility.queryAppSettings(Utility.java:822)
11-18 15:57:54.186 23052-23093/com.creaa.admin.mylogin E/AndroidRuntime: at com.facebook.login.widget.LoginButton$1.run(LoginButton.java:489)
11-18 15:57:54.186 23052-23093/com.creaa.admin.mylogin E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
11-18 15:57:54.186 23052-23093/com.creaa.admin.mylogin E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
11-18 15:57:54.186 23052-23093/com.creaa.admin.mylogin E/AndroidRuntime: at java.lang.Thread.run(Thread.java:818)
以下是我的主要活动:
public class MainActivity extends Activity {
private static String APP_ID = "1500167156975715"; // Replace your App ID here
CallbackManager callbackManager;
ArrayList<String> permissions;
Context cv = this;
boolean loginflag = false;
private ProfileTracker profiletracker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//initilise fb sdk
FacebookSdk.sdkInitialize(getApplicationContext());
//logout any previous logins
AccessToken accessToken = AccessToken.getCurrentAccessToken();
accessToken.setCurrentAccessToken(null);
Profile.getCurrentProfile().setCurrentProfile(null);
LoginManager.getInstance().logOut();
setContentView(R.layout.activity_main);
permissions = new ArrayList<String>();
permissions.add("email");
permissions.add("user_likes");
permissions.add("user_friends");
permissions.add("public_profile");
permissions.add("user_birthday");
try {
PackageInfo info = getPackageManager().getPackageInfo(
"com.example.facebooklogin",
PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.d("Parth", "KeyHash : " + Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
} catch (NameNotFoundException e) {
} catch (NoSuchAlgorithmException e) {
}
callbackManager = CallbackManager.Factory.create();
LoginButton loginbutton = (LoginButton) findViewById(R.id.login_button);
loginbutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (AccessToken.getCurrentAccessToken() != null && Profile.getCurrentProfile() != null) {
if (loginflag) {
AccessToken accessToken = AccessToken.getCurrentAccessToken();
accessToken.setCurrentAccessToken(null);
Profile.getCurrentProfile().setCurrentProfile(null);
Toast.makeText(cv, "Logout", Toast.LENGTH_SHORT).show();
LoginManager.getInstance().logOut();
}
} else {
if (!loginflag) {
Toast.makeText(cv, "Login", Toast.LENGTH_SHORT).show();
LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, permissions);
}
}
}
});
loginbutton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult result) {
// TODO Auto-generated method stub
Toast.makeText(cv, "in onSucess", Toast.LENGTH_SHORT).show();
//get account details
GraphRequest request = GraphRequest.newMeRequest(result.getAccessToken(), new GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
// TODO Auto-generated method stub
Log.d("Parth", response.toString() + "\njson" + object.toString());
try {
String email = (String) object.get("email");
Toast.makeText(cv, email, Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender,birthday");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onError(FacebookException error) {
// TODO Auto-generated method stub
Toast.makeText(cv, "in onError", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancel() {
// TODO Auto-generated method stub
Toast.makeText(cv, "in oncancel", Toast.LENGTH_SHORT).show();
}
});
profiletracker = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
// TODO Auto-generated method stub
if (currentProfile != null) {
Toast.makeText(cv, "in Currentprofilechanged", Toast.LENGTH_SHORT).show();
String name = currentProfile.getName();
String fname = currentProfile.getFirstName();
Toast.makeText(MainActivity.this, fname, Toast.LENGTH_SHORT).show();
}
}
};
}
}
下面是app gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.creaa.admin.mylogin"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
repositories { mavenCentral() }
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.facebook.android:facebook-android-sdk:4.6.0'
}
下面的是清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.creaa.admin.mylogin" >
<uses-permission android:name="android.permission.INTERNET"/>
<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:label="@string/app_name"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="1500167156975715"/>
<provider
android:name="com.facebook.FacebookContentProvider"
android:authorities="com.facebook.app.FacebookContentProvider233936543368280"
android:exported="true" />
</application>
</manifest>
我已经尝试了所有可能的事情,但我无法弄清楚错误。如果有人能帮助我解决这个问题,那将是非常友好的。我刚刚从eclipse转移到了android studio。这段代码在eclipse中运行得很好,但它在android studio中不起作用。
答案 0 :(得分:22)
string.xml
<string name="facebook_app_id">XXXXXX</string>
的manifest.xml
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id"/>
答案 1 :(得分:4)
我终于找到了问题并提出了修复,问题是登录按钮无法找到应用程序应用程序ID,您只需要将应用程序ID元标记放在应用程序标记元素中。这就是全部
答案 2 :(得分:0)
在Login-manager回调中,检查是否存在错误。如是。处理它。
对于每个权限,都会调用回调函数。检查用户已批准的权限并相应地处理信息。
答案 3 :(得分:0)
如果您在片段中实现此功能,请执行以下操作:
loginbutton.setfragment(this)
希望这有帮助。