android 用intent拨打电话 为什么不能拨出 电话号码是unknown?

移动开发 码拜 9年前 (2015-04-25) 772次浏览 0个评论

android新手遇到点小问题,请求大家帮助~

代码就是书上的
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
    xmlns:tools=”http://schemas.android.com/tools”
    android:layout_width=”match_parent”
    android:layout_height=”match_parent”
    android:orientation=”vertical” >

    <EditText
        android:id=”@+id/editText1″
        android:layout_width=”match_parent”
        android:layout_height=”wrap_content”
        android:inputType=”phone”
        android:textColor=”@android:color/black”
        android:textSize=”25px”>
        <requestFocus />
    </EditText>
    <Button
        android:id=”@+id/button”
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:text=”拨打电话”
        android:textColor=”@android:color/black”
        android:textSize=”25px”/>
</LinearLayout>

Activity的是

package com.callbyintent;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        EditText numberTV=(EditText)findViewById(R.id.editText1);
        final String number=numberTV.getText().toString();
        Button dial=(Button)findViewById(R.id.button);
        dial.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setAction(Intent.ACTION_CALL);
                intent.setData(Uri.parse(“tel:”+number));
                startActivity(intent);
                
            }
        });
    }
}

权限也加了 

如果把 intent.setData(Uri.parse(“tel:”+number));改称 intent.setData(Uri.parse(“tel://10086));
或者final String number=numberTV.getText().toString();改称final String number=“10086”;
就都一切正常

如果按照原来代码运行  不显示拨号界面  看通话记录就是unknown
感觉就是EditText不识别输入的数字,这是什么原因?

android 用intent拨打电话 为什么不能拨出 电话号码是unknown?
10分
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button dial=(Button)findViewById(R.id.button);
        dial.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
				EditText numberTV=(EditText)findViewById(R.id.editText1);
				String number=numberTV.getText().toString();
                Intent intent=new Intent();
                intent.setAction(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:"+number));
                startActivity(intent);
                
            }
        });
    }
android 用intent拨打电话 为什么不能拨出 电话号码是unknown?
引用 1 楼 hjywyj 的回复:
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button dial=(Button)findViewById(R.id.button);
        dial.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
				EditText numberTV=(EditText)findViewById(R.id.editText1);
				String number=numberTV.getText().toString();
                Intent intent=new Intent();
                intent.setAction(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:"+number));
                startActivity(intent);
                
            }
        });
    }

真的可以呢  可是为什么呢?
为什么教程给的例程序直接运行就可以,
自己写了一遍就需要这么改动以下呢?

android 用intent拨打电话 为什么不能拨出 电话号码是unknown?
5分
引用 2 楼 feng299 的回复:

真的可以呢  可是为什么呢?
为什么教程给的例程序直接运行就可以,
自己写了一遍就需要这么改动以下呢?

因为你把
EditText numberTV=(EditText)findViewById(R.id.editText1);
        final String number=numberTV.getText().toString();
写在oncreate里了
而在你往edittext里输入前都是空的,所以这样获得的number是空值

你可以Log,或toast一下你那种写法中number的值

android 用intent拨打电话 为什么不能拨出 电话号码是unknown?
5分
只能说教程坑了你
初始化控件的时候就去拿数据 EditText 肯定是空值 因为那时候你还没有输入号码
android 用intent拨打电话 为什么不能拨出 电话号码是unknown?
引用 4 楼 u011070145 的回复:

只能说教程坑了你
初始化控件的时候就去拿数据 EditText 肯定是空值 因为那时候你还没有输入号码

对哈~ 
思考的太少
感谢 ~!   感谢二位的回答~

android 用intent拨打电话 为什么不能拨出 电话号码是unknown?
错了 是三位的回答~  谢谢拉~

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明android 用intent拨打电话 为什么不能拨出 电话号码是unknown?
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!