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);
}
}
});
}
错误
尝试调用虚拟方法'void android.widget.ImageView.setOnClickListener(android.view.View $ OnClickListener)' 在空对象引用上
答案 0 :(得分:4)
您想要一个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
是您的ID,它位于您的布局xml文件中。