0
我正在關注Lynda.com上的視頻,並且我正在嘗試在運行時請求權限 。但是,我得到這個錯誤:無法在運行時詢問權限(Android)
java.lang.NullPointerException: 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:226)
這裏是方法出現錯誤,其中:
public void makeCall() {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:09003344556"));
if (ActivityCompat.checkSelfPermission(
PechhulpActivity.this, Manifest.permission.CALL_PHONE) // Line 226 (Error)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(PechhulpActivity.this,
Manifest.permission.CALL_PHONE)) {
Toast.makeText(PechhulpActivity.this, "I know you said no, but I'm asking again", Toast.LENGTH_SHORT).show();
}
ActivityCompat.requestPermissions(PechhulpActivity.this,
new String[]{Manifest.permission.CALL_PHONE},
MY_PERMISSIONS_REQUEST_CALL_PHONE);
return;
}
startActivity(callIntent);
}
我希望有人知道爲什麼會出現這種錯誤。這是我的全班的要點: https://gist.github.com/soufyanekaddouri/812f65669847619e3af7016403b1e7be
請你能不能給我一個例子嗎? – Stackingcoder
我編輯了答案並添加了一個示例。 –