android - 我有一个图像列表。单击每个 imageView 时,我想从该应用程序进行调用。但我收到错误

标签 android

 int[] imgid = {R.drawable.call, R.drawable.call, R.drawable.call, R.drawable.call, R.drawable.call, R.drawable.call, R.drawable.call, R.drawable.call};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pick_up);

    for (int i = 0; i < imgid.length; i++) {

        ImageView phnImg = (ImageView) findViewById(imgid[i]);

调用电话

        phnImg.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                final int REQUEST_PHONE_CALL = 1;
                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:123456789"));

                //checks for permission before placong the call
                if (ActivityCompat.checkSelfPermission(PickUpActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling

                    ActivityCompat.requestPermissions(PickUpActivity.this, new String[]{Manifest.permission.CALL_PHONE}, REQUEST_PHONE_CALL);
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.

                } else {

                    startActivity(callIntent);

                }
            }
        });
    }

错误

Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

最佳答案

你想要一个 id 列表,但是你的代码得到了一个 drawable 列表,这就是你得到这个错误的原因。

int[] imgid = {R.drawable.call, R.drawable.call, R.drawable.call, R.drawable.call, R.drawable.call, R.drawable.call, R.drawable.call, R.drawable.call};

所以请更正如下

int[] imgid = {R.id.image_id, R.id.image_id, R.id.image_id, R.id.image_id, R.id.image_id, R.id.image_id, R.id.image_id, R.id.image_id};

image_id 是您的布局 xml 文件中的 ID。

关于android - 我有一个图像列表。单击每个 imageView 时,我想从该应用程序进行调用。但我收到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51569631/

相关文章:

android - 如何从 GPUImageView 中删除过滤器

Android镜像矢量可绘制

android - 如何从Android上的给定地址获取纬度和经度?

java - 使用 TCP 连接无法从 Android 接收数据

android - 在为较早的查询迭代游标的同时执行新的 SQLite 查询是否安全? (安卓)

android - ML Kit - Android - 文本识别 - 文本方向

java - 动画添加到android ListView ?

Java - 使用 TCP 套接字接收通知

android - 加载比 android ListView 中可见的更多的项目

android - google sheet 自定义菜单功能在 android 上的 google sheet 中不可见