2016-07-31 40 views
0

我試圖[執行一點點權限檢查的測試,但它 給了我這個錯誤:爲什麼checkSelfPermissions()返回null? (安卓)

Attempt to invoke virtual method 'int android.content.Context.checkPermission(java.lang.String, int, int)' on a null object reference at com.doppler.stackingcoder.pechhulp.PechhulpActivity.makeCall(PechhulpActivity.java:238)

這裏是我的方法,其中出現的錯誤:

@TargetApi(Build.VERSION_CODES.M) 
public void makeCall() { 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) { 
     startActivity(callIntent); 
    } else { 
     Toast.makeText(this, "Geen toestemming om te bellen", Toast.LENGTH_SHORT).show(); 
    } 

} 

我希望有人知道這個問題,提前致謝!

編輯:全班同學:https://gist.github.com/soufyanekaddouri/6363463ef1fb1988af4fb6f9e7e8b228

我打電話的方法,(MakeCall函數)從另一個類,稱爲CustomDialogClass.java。這裏要說的是類的要點:

https://gist.github.com/soufyanekaddouri/bcf045f80e4f9c9b95994dafdf1ba682

+0

? –

+0

Android 6.0所以我認爲23級 – Stackingcoder

回答

0

API等級23推出,您可以使用checkSelfPermission方法從活動實例(請確保您使用的活動的實例,而不是的靜態參考活動課)。

yourActivityInstance.checkSelfPermission(String permision); 

特別是在你的情況:

@TargetApi(Build.VERSION_CODES.M) 
public void makeCall() { 
    Intent callIntent = new Intent(Intent.ACTION_CALL); 
    callIntent.setData(Uri.parse("tel:09003344556")); 
    boolean hasCallPhonePermission = this.checkSelfPermission(Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED; 
    if (!hasCallPhonePermission){ 
     // The app does not have permission to phone call ! 
     return; 
    } 
    // The app has permission to phone call 
} 
您正在使用什麼Android的API級別
+0

我的ActivityInstance是什麼? – Stackingcoder

+0

如果您的makeCell方法位於您的Activity類中並且該Activity正在運行,則可以使用'this'來引用當前的Activity實例。 –

+0

是的我正在嘗試,但它給了我這個錯誤:ContextWrapper中的checkSelfPermission無法應用編輯:現在我剛剛插入活動名稱:PechhulpActivity,但現在它給了我這個紅色的下劃線錯誤:非靜態方法checkSelfPermission不能被引用一個靜態的上下文 – Stackingcoder