gpt4 book ai didi

java - 为什么 checkSelfPermission() 抛出 NullPointerException

转载 作者:行者123 更新时间:2023-12-02 09:46:58 28 4
gpt4 key购买 nike

当按下 ListView 中的通话按钮时,我正在检查电话通话权限。由于某种原因,我在 checkSelfPermission() 方法中作为参数传递的 Context 导致了空指针异常。我使用过 thisActivityName.thisgetContext()getApplicationContext()。它们都不适合我。

注意:makephonecall() 方法的调用是在按下 ListView 按钮时从我的适配器类发起的。

public void makephonecall()
{
try {
if (ContextCompat.checkSelfPermission(BloodVaultActivity2.this,
Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {

ActivityCompat.requestPermissions(BloodVaultActivity2.this,
new String[]{Manifest.permission.CALL_PHONE}, REQUEST_CALL);
} else {
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + number));
startActivity(intent);
}
}
catch (NullPointerException e){
e.printStackTrace();
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

if(requestCode == REQUEST_CALL)
{
if(grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
makephonecall();
}
else
{
Toast.makeText(getApplicationContext(), "Permission Denied", Toast.LENGTH_SHORT).show();
}
}
}


适配器类:

package bd.edu.bubt.regup;

import android.*;
import android.Manifest;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class BloodVaultAdapter extends ArrayAdapter<BloodVaultItem> {

ArrayList<BloodVaultItem> adapterlist = new ArrayList<>();
int REQUEST_CALL = 1;

public BloodVaultAdapter(Context context, int textViewResourceId, ArrayList<BloodVaultItem> objects){

super(context, textViewResourceId, objects);
adapterlist = objects;
}

@Override
public int getCount(){

return super.getCount();
}

@Override
public View getView(final int position, View convertView, ViewGroup parent){

View v = convertView;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.blood_vault_list_view_layout, null);

TextView name = (TextView) v.findViewById(R.id.name);
TextView group = (TextView) v.findViewById(R.id.group);
Button call = (Button) v.findViewById(R.id.call);

call.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

BloodVaultItem bloodVaultItem = getItem(position);

String number = bloodVaultItem.getContact();

BloodVaultActivity2 bloodVaultActivity2 = new BloodVaultActivity2();
bloodVaultActivity2.number = number;
bloodVaultActivity2.makephonecall();
}
});

name.setText("Name: " +adapterlist.get(position).getName());
group.setText("Blood Group: " +adapterlist.get(position).getGroup());

return v;
}
}


Logcat:

06-13 01:42:36.920 13102-13102/bd.edu.bubt.regup W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Context.checkPermission(java.lang.String, int, int)' on a null object reference
06-13 01:42:36.921 13102-13102/bd.edu.bubt.regup W/System.err: at android.support.v4.content.ContextCompat.checkSelfPermission(ContextCompat.java:430)
06-13 01:42:36.921 13102-13102/bd.edu.bubt.regup W/System.err: at bd.edu.bubt.regup.BloodVaultActivity2.makephonecall(BloodVaultActivity2.java:182)
06-13 01:42:36.921 13102-13102/bd.edu.bubt.regup W/System.err: at bd.edu.bubt.regup.BloodVaultAdapter$1.onClick(BloodVaultAdapter.java:63)
06-13 01:42:36.921 13102-13102/bd.edu.bubt.regup W/System.err: at android.view.View.performClick(View.java:5637)
06-13 01:42:36.921 13102-13102/bd.edu.bubt.regup W/System.err: at android.view.View$PerformClick.run(View.java:22429)
06-13 01:42:36.921 13102-13102/bd.edu.bubt.regup W/System.err: at android.os.Handler.handleCallback(Handler.java:751)
06-13 01:42:36.922 13102-13102/bd.edu.bubt.regup W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
06-13 01:42:36.922 13102-13102/bd.edu.bubt.regup W/System.err: at android.os.Looper.loop(Looper.java:154)
06-13 01:42:36.922 13102-13102/bd.edu.bubt.regup W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6119)
06-13 01:42:36.922 13102-13102/bd.edu.bubt.regup W/System.err: at java.lang.reflect.Method.invoke(Native Method)
06-13 01:42:36.922 13102-13102/bd.edu.bubt.regup W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
06-13 01:42:36.922 13102-13102/bd.edu.bubt.regup W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
06-13 01:42:36.928 1382-1455/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 137954329 , only wrote 137954160
06-13 01:42:36.975 1382-1455/? W/audio_hw_generic: Hardware backing HAL too slow, could only write 0 of 720 frames
06-13 01:42:40.154 1382-1456/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 138261974 , only wrote 138108960
06-13 01:43:28.433 2223-8977/com.google.android.apps.nexuslauncher I/ClearcutLoggerApiImpl: disconnect managed GoogleApiClient
01-01 00:00:00.000 0-0/? E/Internal: device offline

最佳答案

尝试传递上下文(在适配器的构造函数中接收)来代替BloodVaultActivity2.this,即

ContextCompat.checkSelfPermission(context,Manifest.permission.CALL_PHONE)

此外,BloodVaultActivity2 BloodVaultActivity2 = new BloodVaultActivity2();是不正确的调用方法的方式,因为这将创建您的 Activity 的新实例,而是将您的 context 类型转换为 BloodVaultActivity2 并从您的 Activity 中传递 this适配器的构造函数:

if(mContext instanceof YourActivityName){
((YourActivityName)mContext).yourActivityMethod();
}

顺便说一句, Activity 和适配器之间交互的更好方法是使用接口(interface)。 https://android.jlelse.eu/click-listener-for-recyclerview-adapter-2d17a6f6f6c9

关于java - 为什么 checkSelfPermission() 抛出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56572686/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com