android - Android 中的调用 Intent

标签 android android-intent android-implicit-intent

我怎样才能通过按下按钮调用电话?我从 EditText 获取我的号码作为字符串。这是我的示例代码:

String phone = editPhone.getText().toString();
btnPhone.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                call();
            }
        });
public void call() {
    try {
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse(phone));
        startActivity(callIntent);
    } catch (ActivityNotFoundException activityException) {
         Log.e("myphone dialer", "Call failed", e);
    }
}

我向 list 文件添加了所有权限

但我得到了 NullPointerexception

最佳答案

这种简单的方法应该适合您。

例如

public class CallActivity extends Activity{
   String phone = "";

   onCreate()
   {
        btnPhone.setOnClickListener(new View.OnClickListener() { 
            @Override 
            public void onClick(View arg0) { 
                phone = editPhone.getText().toString(); 
                call(); 
            } 
        });    
   }

   public void call() {   
            Intent callIntent = new Intent(Intent.ACTION_CALL);          
            callIntent.setData(Uri.parse("tel:"+phone));          
            startActivity(callIntent);  
   }
}

您可能正在超出范围使用字符串变量 phone

关于android - Android 中的调用 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10510395/

相关文章:

AFW 注册后,Android O 无法扩充 webview

android - 如何在其他类中从 MainActivity 调用(和使用)onOptionsItemSelected

android - "Error, could not load media"尝试调用照片应用裁剪 Intent 发送指向文件目录的 Uri

android - 在 Android 中实现文件选择器并将所选文件复制到另一个位置

android - 将 PDF 文件作为邮件发送或提供应用程序以直接查看文件

Android Material Design 分隔线颜色

android - 如何使用 Bundle 在 Activity 之间传递 Uri 数组

Android 服务没有以警告开始

android - 改变 Toast 的颜色

android - 重定向到 Intent Activity 时出现问题