2014-12-23 13 views
-1

我想打电话时,我点击确定按钮。现在我得到错误如何在android片段类中调用手机?

错误消息

12-23 17:19:39.547: E/AndroidRuntime(4095): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.CALL dat=91 90-37-300100 flg=0x10000000 } 

这里是我的代码

public class ContactFragment extends Fragment { 
private View parentView; 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    //return inflater.inflate(R.layout.contact, container, false); 
    parentView = inflater.inflate(R.layout.contact, container, false); 
    setUpViews(); 
    return parentView; 
} 

private void setUpViews() { 
    parentView.findViewById(R.id.contact_phone).setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 
      // Creating alert Dialog with two Buttons 
      AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity()); 
      // Setting Dialog Title 
      alertDialog.setTitle("Do you want to call?"); 
      // Setting Dialog Message 
      alertDialog.setMessage("+91 XXXXXXXXXX"); 
      // Setting Icon to Dialog 
      //alertDialog.setIcon(R.drawable.warning); 
      // Setting Negative "NO" Button 
      alertDialog.setNegativeButton("No", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, 
           int which) { 
          // Write your code here to execute after dialog 
          dialog.cancel(); 
         } 
        }); 
      // Setting Positive "Yes" Button 
      alertDialog.setPositiveButton("Yes", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, 
           int which) { 
          // Write your code here to execute after dialog 
          Intent callIntent = new Intent(Intent.ACTION_CALL); 
          callIntent.setData(Uri.parse("91 XXXXXXXXXX")); 
          callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
          startActivity(callIntent); 

         } 
        }); 

      // Showing Alert Message 
      alertDialog.show(); 
     } 
    }); 
} } 

回答

1

更改此:

callIntent.setData(Uri.parse("91 XXXXXXXXXX")); 

与此:

callIntent.setData(Uri.parse("tel:" + "91XXXXXXXXXX")); 
+0

其工作..非常感谢。 –

1

变化

callIntent.setData(Uri.parse("91 XXXXXXXXXX")); 

callIntent.setData(Uri.parse("tel:" + "91 XXXXXXXXXX")); 

只需在添加号码前添加“tel:”

所以基本上你必须调用下面的代码。

Intent intent = new Intent(Intent.ACTION_DIAL); 
intent.setData(Uri.parse("tel:"+phone)); 
startActivity(intent); 
3

只是替换代码:

Intent callIntent = new Intent(Intent.ACTION_CALL); 
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
callIntent.setData(Uri.parse("tel:" + "91XXXXXXXXXX")); 
getActivity().startActivity(callIntent); 

和呼叫添加权限清单文件

+0

这是正确的答案。你不能使用startActivity(callIntent);直接从片段。您必须先使用getActivity检索活动,然后使用此处给出的startActivity(callIntent)。 –