如何在阵列适配器中Intent.ACTION_CALL Android面对活动未找到异常



我正在使用阵列适配器在列表视图中加载数据。 在列表视图中,我有电话号码,如果用户点击该号码,调用功能应该会发生。

我的数组适配器类如下

public class PendingDeliveryAdapter extends ArrayAdapter<PendingDeliveryPojoClass>{
private LayoutInflater inflater;
private Context context;
public PendingDeliveryAdapter(Context context,List<PendingDeliveryPojoClass> data)
{
    super(context,R.layout.listview_pending_delivery,data);
    this.context = context;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

并获取视图方法等...

在按钮单击中调用调用函数的代码

@Override
        public void onClick(View view) {
             Intent phoneIntent = new Intent(Intent.ACTION_CALL);
              phoneIntent.setData(Uri.parse(holder.delivery_phone_number.getText().toString()));
              try {
                 context.startActivity(phoneIntent);
              } catch (android.content.ActivityNotFoundException ex) {
                 Toast.makeText(getContext(), 
                 "Call faild, please try again later.", Toast.LENGTH_SHORT).show();
              }
        }

我面临活动未找到异常。 如何解决这个问题?

Intent.ACTION_CALL 的正确用法是

Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + number));
startActivity(intent);

因此,您正在设置的意图数据中缺少tel:。为了使你的代码工作,改变

phoneIntent.setData(Uri.parse(holder.delivery_phone_number.getText().toString()));

phoneIntent.setData(Uri.parse("tel:" + holder.delivery_phone_number.getText().toString()));

你必须这样称呼意图

@Override
public void onClick(View view) {
     Intent callIntent = new Intent(Intent.ACTION_CALL);
     callIntent.setData(Uri.parse("tel:" + temp1));
     context.startActivity(callIntent);

}

但是您在初始化意向Intent.CALL_ACTION后是否尝试过此操作?

 Intent callIntent = new Intent(Intent.ACTION_CALL);
 callIntent .setPackage("com.android.phone");
 callIntent.setData(Uri.parse("tel:" + temp1));
 context.startActivity(callIntent);

指定包名称?

希望它对你有用

在您的适配器中输入以下代码:

String phoneNumber = editPhoneNumber.getText().toString();
Strin uri = phoneNumber.trim();
Uri call = Uri.parse("tel:" + uri);
intent = new Intent(Intent.ACTION_CALL);
intent.setData(call);
itemView.getContext().startActivity(intent);

最新更新