Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 单击按钮获取权限时,应用程序已停止_Android_Xml - Fatal编程技术网

Android 单击按钮获取权限时,应用程序已停止

Android 单击按钮获取权限时,应用程序已停止,android,xml,Android,Xml,当我点击按钮授予我的应用程序权限时,会显示停止按摩和权限消息。因此,如果我同意授予权限,我的应用程序已经停止,但如果我再次打开它,它将工作得很好 但如果我否认,它将停止,直到我同意 我在logcat中也没有发现任何想法 我的XML代码: 你的应用程序崩溃了,因为在点击下面的if条件后,它继续使用你的代码,而你没有授予该应用程序权限。您应该添加onRequestPermissionsResult作为回调,并将电话代码移动到单独的方法。如果授予了权限,则可以调用它 myBtn.setOn

当我点击按钮授予我的应用程序权限时,会显示停止按摩和权限消息。因此,如果我同意授予权限,我的应用程序已经停止,但如果我再次打开它,它将工作得很好 但如果我否认,它将停止,直到我同意

我在logcat中也没有发现任何想法

我的XML代码:


你的应用程序崩溃了,因为在点击下面的if条件后,它继续使用你的代码,而你没有授予该应用程序权限。您应该添加
onRequestPermissionsResult
作为回调,并将电话代码移动到单独的方法。如果授予了权限,则可以调用它

     myBtn.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    if(ContextCompat.checkSelfPermission(MainActivity.this,
                            Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
                        telephonyStuff();
                    } else {
                        requestReadPhonePermission();
                    }
                };// move the things you do with the telephony in the callback;
             }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case  Phone_State_Permission_Code: {

              // If request is cancelled, the result arrays are empty.
              if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    // permission was granted, yay! Do the
                    // contacts-related task you need to do. 

                     telephonyStuff();
                } else {

                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                    //handle the situation if the permission was denied;
                }
                return;
            }

            // other 'case' lines to check for other
            // permissions this app might request
        }
    }

//create new method 

    public void telephonyStuff() {
         TelephonyManager tm=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

                        String IMEInum;
                        IMEInum=tm.getDeviceId();
                        IMEI.setText("IMEI: "+IMEInum);


                        String lineNum = tm.getLine1Number();
                        Linenum.setText("Line number: "+lineNum);

                        int phoneType=tm.getPhoneType();
                        switch (phoneType)
                        {
                            case (TelephonyManager.PHONE_TYPE_CDMA):
                                Phonetype.setText("Phone type: CDMA");
                                break;
                            case (TelephonyManager.PHONE_TYPE_GSM):
                                Phonetype.setText("Phone type: GSM");
                                break;
                            case (TelephonyManager.PHONE_TYPE_NONE):
                                Phonetype.setText("Phone type: NONE");
                                break;
                        }

                        int CallState = tm.getCallState();
                        switch (CallState) {
                            case TelephonyManager.CALL_STATE_IDLE:
                                Call.setText("CALL STATE : IDLE");
                                break;
                            case TelephonyManager.CALL_STATE_OFFHOOK:
                                Call.setText("CALL STATE : OFFHOOK");
                                break;
                            case TelephonyManager.CALL_STATE_RINGING:
                                Call.setText("CALL STATE : RINGING");
                                break;
                        }

                        int SIMState=tm.getSimState();
                        switch(SIMState)
                        {
                            case TelephonyManager.SIM_STATE_ABSENT :
                                Sim.setText("SIM state: ABSENT");
                                break;
                            case TelephonyManager.SIM_STATE_NETWORK_LOCKED :
                                Sim.setText("SIM state: NETWORK LOCKED");
                                break;
                            case TelephonyManager.SIM_STATE_PIN_REQUIRED :
                                Sim.setText("SIM state: PIN REQUIRED");
                                break;
                            case TelephonyManager.SIM_STATE_PUK_REQUIRED :
                                Sim.setText("SIM state: PUK REQUIRED");
                                break;
                            case TelephonyManager.SIM_STATE_READY :
                                Sim.setText("SIM state: READY");
                                break;
                            case TelephonyManager.SIM_STATE_UNKNOWN :
                                Sim.setText("SIM state: UNKNOWN");
                                break;

                        }

    }

不确定这与JavaScript有什么关系?