Java 始终生成异常

Java 始终生成异常,java,android,android-studio,android-6.0-marshmallow,Java,Android,Android Studio,Android 6.0 Marshmallow,每当我在emulator(任何版本)或实际设备中运行此代码时,它总是抛出异常并显示消息。 经过一些研究,它似乎需要api 23中的运行时权限,因此我在api 22和api 15设备中运行了代码,但问题仍然存在。 有什么解决方案吗?看起来您正在尝试拨打一个电话号码。 如果是这样,你的意图是错误的 URI的前缀应为tel: private void call() { intent in=new Intent(Intent.ACTION_CALL,Uri.parse("9424863135"

每当我在emulator(任何版本)或实际设备中运行此代码时,它总是抛出异常并显示消息。 经过一些研究,它似乎需要api 23中的运行时权限,因此我在api 22和api 15设备中运行了代码,但问题仍然存在。
有什么解决方案吗?

看起来您正在尝试拨打一个电话号码。 如果是这样,你的意图是错误的

URI的前缀应为
tel:

private void call() 
{
    intent in=new Intent(Intent.ACTION_CALL,Uri.parse("9424863135"));
    try
    {
        startActivity(in);
    }
    catch (android.content.ActivityNotFoundException ex)
    {
        Toast.makeText(getApplicationContext(),"yourActivity is not founded",Toast.LENGTH_SHORT).show();
    }
}

是的,在api 23中,您需要请求运行时许可才能拨打电话。 要了解如何请求运行时权限,请检查以下链接

点击按钮拨打电话,写下以下代码:

intent in=new Intent(Intent.ACTION_CALL,Uri.parse("tel:9424863135"));
并按如下方式处理回调结果:

private void checkCallingPermission() {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE)
            != PackageManager.PERMISSION_GRANTED) {

        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.CALL_PHONE)) {
            Toast.makeText(ContactUsActivity.this, "Permission is needed for proper working of this app", Toast.LENGTH_SHORT).show();
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, REQUEST_CODE_CALL);
        } else {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, REQUEST_CODE_CALL);
        }

    } else {
        call();
    }
}
这将是进行调用的方法

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == REQUEST_CODE_CALL) {
        if (grantResults.length == 1
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            call();
        } else {
            Toast.makeText(ContactUsActivity.this, "Permission is needed for proper working of this app", Toast.LENGTH_SHORT).show();
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, REQUEST_CODE_CALL);
        }
    }
}

哪个例外。。。堆栈跟踪?请为您的问题提供更详细的标题。另外,发布错误日志会很有帮助。另外,您是否已将所需的权限添加到您的清单中?@如果您添加了,请删除否决票,因为问题已经解决answered@awadheshsingh重命名您的帖子,如果其他人试图了解调用所需的权限,那么如何使用名为“始终生成异常”的帖子?我不会取消我的否决票,除非这是一个对其他人有用的问题和答案,而不仅仅是要求其他人解决自己眼前的问题。如有要求,堆栈跟踪对其他面临类似问题的人也很有用。@该人员建议我将对其进行编辑是的,这是我一直在寻找的答案,但我在阅读链接后仍不理解。请修复我的代码并重新发布。我已编辑了我的答案,添加了您需要的代码。检查并不要忘记将此答案标记为已接受。
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == REQUEST_CODE_CALL) {
        if (grantResults.length == 1
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            call();
        } else {
            Toast.makeText(ContactUsActivity.this, "Permission is needed for proper working of this app", Toast.LENGTH_SHORT).show();
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, REQUEST_CODE_CALL);
        }
    }
}
private void call() {
    String url = "tel:" + "12234455";
    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
    startActivity(intent);
}