【发布时间】:2018-02-06 09:59:27
【问题描述】:
这是主要活动。
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText number;
Button makecall;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number = findViewById(R.id.number);
makecall = findViewById(R.id.makecall);
call();
}
private void call() {
makecall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent;
intent = new Intent(Intent.ACTION_DIAL);
intent = intent.setData(Uri.parse("number:"+number.getText().toString()));
startActivity(intent);
}
});
}
}
我将此权限请求添加到清单文件。
<uses-permission android:name="android.permission.CALL_PHONE"/>
这是我的 XML 文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.bimstajyer1.calisma10.MainActivity">
<EditText
android:id="@+id/number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Number"
android:inputType="number"
android:maxLength="11" />
<Button
android:id="@+id/makecall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Make a call" />
</LinearLayout>
当我按下通话按钮拨打电话时出现此错误。
致命异常:主要
进程:com.example.bimstajyer1.calisma10,PID:25198 android.content.ActivityNotFoundException:找不到要处理的活动 意图 { act=android.intent.action.DIAL dat=number: }
错误行是:startActivity(intent);
【问题讨论】:
-
你为什么打电话给你的电话();方法来自 onCreate() ???,在 onCreate() 中的 Button 中设置点击侦听器,验证您的号码,然后调用您的 call() 方法。
标签: java android android-activity