我想在我的Android应用程序中集成Facebook。
我已按照this link中的步骤进行操作。
MainActivity :
public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_login);
try {
PackageInfo info = getPackageManager().getPackageInfo(
"com.facebook.samples.loginhowto",
PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
} catch (Exception e) {
}
}
}
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.facebook.android" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".LoginActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="App_id_Value"/>
</application>
<!-- Connect to Internet Permissions -->
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
Log cat是:
09-03 13:12:49.240 18433-18433/? E/Zygote﹕ MountEmulatedStorage()
09-03 13:12:49.240 18433-18433/? E/Zygote﹕ v2
09-03 13:12:49.245 18433-18433/? E/SELinux﹕ [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
09-03 13:12:49.740 18433-18480/? E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #4
Process: com.facebook.android, PID: 18433
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:825)
at com.facebook.login.widget.LoginButton$1.run(LoginButton.java:489)
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)
答案 0 :(得分:1)
问题出在这里
PackageInfo info = getPackageManager().getPackageInfo(
"com.facebook.samples.loginhowto",
PackageManager.GET_SIGNATURES);
您必须编写清单中指定的应用包。在这种情况下:
PackageInfo info = getPackageManager().getPackageInfo(
"com.facebook.android",
PackageManager.GET_SIGNATURES);
答案 1 :(得分:0)
您在manifest.xml和代码中使用了不同的包名称来生成哈希密钥。
尝试这样:
private void getHashKey() {
try {
PackageInfo info = getPackageManager().getPackageInfo(
getPackageName(), PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.d("RAJAT",
"KeyHash: "
+ Base64.encodeToString(md.digest(),
Base64.DEFAULT));
}
} catch (NameNotFoundException e) {
} catch (NoSuchAlgorithmException e) {
}
}