2016-03-12 79 views
0

我使用此代碼,讓我發展的關鍵哈希的Facebook SDK產生keyhash

public class Myap extends Application { 
    @Override 
    public void onCreate() { 
     super.onCreate(); 

    } 
    public void printHashkey() 
    { 
     // Add code to print out the key hash 
     try { 
      PackageInfo info = getPackageManager().getPackageInfo(
        "com.vitmail.android", 
        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 (PackageManager.NameNotFoundException e) { 

     } catch (NoSuchAlgorithmException e) { 

     } 
    } 
} 

但它每次生成相同的錯誤

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vitmail.android/com.vitmail.android.navbar_fragments.NavigationActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference 

請告訴我哪裏是錯誤的?

+0

你必須在stackoverflow上搜索'什麼是java.lang.NullPointerException'。 – ELITE

回答

1

試試我的代碼.............

public void getFbKeyHash(String packageName) { 

     try { 
      PackageInfo info = getPackageManager().getPackageInfo(
        packageName, 
        PackageManager.GET_SIGNATURES); 
      for (Signature signature : info.signatures) { 
       MessageDigest md = MessageDigest.getInstance("SHA"); 
       md.update(signature.toByteArray()); 
       Log.d("YourKeyHash :", Base64.encodeToString(md.digest(), Base64.DEFAULT)); 
       System.out.println("YourKeyHash: " + Base64.encodeToString(md.digest(), Base64.DEFAULT)); 
      } 
     } catch (PackageManager.NameNotFoundException e) { 

     } catch (NoSuchAlgorithmException e) { 

     } 

    } 

稱他們onCreate()方法..............

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_login); 
     context = this; 
     getFbKeyHash("com.vitmail.android"); 


    } 
+0

謝謝你!有效 –