我想在我的Android应用程序中添加调用函数。但我的代码无效

时间:2017-05-16 15:54:58

标签: android

我想在我的应用程序中添加调用函数。但我的代码不起作用请指导我。当我尝试运行此代码。我的应用程序崩溃但它没有给出任何错误.... 我的代码在这里

public class ContactUs extends Fragment {
    final ContactUs context = this;
    Button button;
    String sNum;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.contact_us,null);

        button = (Button) v. findViewById(R.id.tvCall);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(Intent.ACTION_CALL);
                //         sNum = numTxt.getText().toString();
                if(sNum.trim().isEmpty()){
                    i.setData(Uri.parse("tel:1234567"));
                } else{ i.setData(Uri.parse("tel:"+sNum));
                }
                if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED)
                {
                    Toast.makeText(getActivity(),"Please grant the permission to call",Toast.LENGTH_SHORT).show(); requestPermission();
                }
                else { startActivity(i);
                }
            }
            private void requestPermission()
            { ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CALL_PHONE},1);
            }

        });

        return v;
    }

我还在我的清单文件中输入权限。我的清单文件在这里

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hp.amibank">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

1 个答案:

答案 0 :(得分:0)

我得到了解决方案...问题是它在sNum变量上返回null。实际上没有numText小部件。当我评论它时,它正在工作..

    public class ContactUs extends Fragment {
//    final ContactUs context = this;
    Button button;
    String sNum;
//    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.contact_us,null);

        button = (Button) v. findViewById(R.id.tvCall);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(Intent.ACTION_CALL);
                //         sNum = numTxt.getText().toString();
                //if(sNum.trim().isEmpty()){
                    i.setData(Uri.parse("tel:1234567"));
               // } //else{ i.setData(Uri.parse("tel:"+sNum));
//                }
                if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED)
                {
                    Toast.makeText(getActivity(),"Please grant the permission to call",Toast.LENGTH_SHORT).show(); requestPermission();
                }
                else { startActivity(i);
                }
            }
            private void requestPermission()
            { ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CALL_PHONE},1);
            }

        });

        return v;
    }