我的应用程序在请求权限时崩溃

My app crashes while requesting permission

您好,我正在制作一个用于控制需要 (WRITE_SETTINGS) 权限的设备屏幕的应用程序,我使用了一种方法来请求权限,但应用程序崩溃了。 我的应用程序检查版本是否高于 android 5 或更高版本它调用一种方法来请求权限但它不会工作,任何帮助?

public class MainActivity extends AppCompatActivity {

      SeekBar sb;
      TextView prog;
      Context context;
      int Brightness;
      boolean pergranted;



      @
      Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);

          int MyVersion = Build.VERSION.SDK_INT;
          if (MyVersion > Build.VERSION_CODES.LOLLIPOP_MR1) {
              if (!checkIfpermissiongranted()) {
                  requestPermission();

              }


              sb = (SeekBar) findViewById(R.id.seekBar);
              prog = (TextView) findViewById(R.id.Progress);
              context = getApplicationContext();


              //Getting current screen brightness
              Brightness = Settings.System.getInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 0);

              //Setting current screen brightness to seekbar
              sb.setProgress(Brightness);

              //Setting current screen brightness to textview
              prog.setText(R.string.current_brightness + Brightness);





              sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {@
                  Override
                  public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                      if (pergranted) {
                          prog.setText(R.string.current_brightness + progress);
                          //Changing brightness on seekbar change
                          Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, progress);
                      }
                  }@
                  Override
                  public void onStartTrackingTouch(SeekBar seekBar) {

                  }

                  @
                  Override
                  public void onStopTrackingTouch(SeekBar seekBar) {

                  }
              });
          }


      }

      private boolean checkIfpermissiongranted() {
          int result = ContextCompat.checkSelfPermission(this, Manifest.permission.GET_ACCOUNTS);
          if (result == PackageManager.PERMISSION_GRANTED) {
              return true;
          } else {
              return false;
          }
      }



      private void requestPermission() {
          ActivityCompat.requestPermissions(this, new String[] {
              Manifest.permission.WRITE_SETTINGS
          }, 1);
      }

      @
      Override
      public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
          switch (requestCode) {
              case 1:
                  if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                      pergranted = true;

                  }
          }


      }

  }

Logcat

08-16 11:08:02.469 24124-24124/com.example.user.eyeprotector I/art: Late-enabling -Xcheck:jni
08-16 11:08:02.598 24124-24124/com.example.user.eyeprotector W/System: ClassLoader referenced unknown path: /data/app/com.example.user.eyeprotector-1/lib/arm
08-16 11:08:02.666 24124-24130/com.example.user.eyeprotector I/art: Debugger is no longer active
08-16 11:08:03.797 24124-24124/com.example.user.eyeprotector W/System: ClassLoader referenced unknown path: /data/app/com.example.user.eyeprotector-1/lib/arm
08-16 11:08:04.086 24124-24124/com.example.user.eyeprotector D/PhoneWindowEx: [PWEx][generateLayout] setLGNavigationBarColor : colors=0xff000000
08-16 11:08:04.086 24124-24124/com.example.user.eyeprotector I/PhoneWindow: [setLGNavigationBarColor] color=0x ff000000
08-16 11:08:04.135 24124-24124/com.example.user.eyeprotector W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
08-16 11:08:04.273 24124-24124/com.example.user.eyeprotector W/ResourceType: For resource 0x7f060073, entry index(115) is beyond type entryCount(22)
08-16 11:08:04.273 24124-24124/com.example.user.eyeprotector W/ResourceType: Failure getting entry for 0x7f060073 (t=5 e=115) (error -75)
08-16 11:08:04.274 24124-24124/com.example.user.eyeprotector D/AndroidRuntime: Shutting down VM
08-16 11:08:04.280 24124-24124/com.example.user.eyeprotector E/AndroidRuntime: FATAL EXCEPTION: main
                                                                               Process: com.example.user.eyeprotector, PID: 24124
                                                                               java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.eyeprotector/com.example.user.eyeprotector.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x7f060073
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
                                                                                   at android.app.ActivityThread.access0(ActivityThread.java:157)
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                   at android.os.Looper.loop(Looper.java:148)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5530)
                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
                                                                                Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x7f060073
                                                                                   at android.content.res.Resources.getText(Resources.java:327)
                                                                                   at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
                                                                                   at android.widget.TextView.setText(TextView.java:4642)
                                                                                   at com.example.user.eyeprotector.MainActivity.onCreate(MainActivity.java:58)
                                                                                   at android.app.Activity.performCreate(Activity.java:6272)
                                                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494) 
                                                                                   at android.app.ActivityThread.access0(ActivityThread.java:157) 
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356) 
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                   at android.os.Looper.loop(Looper.java:148) 
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5530) 
                                                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732) 
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622) 
08-16 11:08:06.401 24391-24391/com.example.user.eyeprotector W/System: ClassLoader referenced unknown path: /data/app/com.example.user.eyeprotector-1/lib/arm
08-16 11:08:06.526 24391-24391/com.example.user.eyeprotector W/System: ClassLoader referenced unknown path: /data/app/com.example.user.eyeprotector-1/lib/arm
08-16 11:08:06.780 24391-24391/com.example.user.eyeprotector D/PhoneWindowEx: [PWEx][generateLayout] setLGNavigationBarColor : colors=0xff000000
08-16 11:08:06.780 24391-24391/com.example.user.eyeprotector I/PhoneWindow: [setLGNavigationBarColor] color=0x ff000000
08-16 11:08:06.815 24391-24391/com.example.user.eyeprotector W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
08-16 11:08:06.906 24391-24391/com.example.user.eyeprotector W/ResourceType: For resource 0x7f060073, entry index(115) is beyond type entryCount(22)
08-16 11:08:06.906 24391-24391/com.example.user.eyeprotector W/ResourceType: Failure getting entry for 0x7f060073 (t=5 e=115) (error -75)
08-16 11:08:06.906 24391-24391/com.example.user.eyeprotector D/AndroidRuntime: Shutting down VM
08-16 11:08:06.907 24391-24391/com.example.user.eyeprotector E/AndroidRuntime: FATAL EXCEPTION: main
                                                                               Process: com.example.user.eyeprotector, PID: 24391
                                                                               java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.eyeprotector/com.example.user.eyeprotector.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x7f060073
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
                                                                                   at android.app.ActivityThread.access0(ActivityThread.java:157)
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                   at android.os.Looper.loop(Looper.java:148)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5530)
                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
                                                                                Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x7f060073
                                                                                   at android.content.res.Resources.getText(Resources.java:327)
                                                                                   at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
                                                                                   at android.widget.TextView.setText(TextView.java:4642)
                                                                                   at com.example.user.eyeprotector.MainActivity.onCreate(MainActivity.java:58)
                                                                                   at android.app.Activity.performCreate(Activity.java:6272)
                                                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494) 
                                                                                   at android.app.ActivityThread.access0(ActivityThread.java:157) 
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356) 
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                   at android.os.Looper.loop(Looper.java:148) 
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5530) 
                                                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732) 
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622) 
08-16 11:08:08.726 24391-24391/com.example.user.eyeprotector I/Process: Sending signal. PID: 24391 SIG: 9
08-16 11:08:11.524 24524-24524/com.example.user.eyeprotector I/Process: Sending signal. PID: 24524 SIG: 9
08-16 11:08:21.581 24793-24793/com.example.user.eyeprotector I/Process: Sending signal. PID: 24793 SIG: 9
08-16 11:08:25.503 24874-24874/com.example.user.eyeprotector I/Process: Sending signal. PID: 24874 SIG: 9
08-16 11:09:43.222 25035-25035/com.example.user.eyeprotector I/art: Late-enabling -Xcheck:jni
08-16 11:09:43.277 25035-25035/com.example.user.eyeprotector W/System: ClassLoader referenced unknown path: /data/app/com.example.user.eyeprotector-1/lib/arm
08-16 11:09:43.370 25035-25035/com.example.user.eyeprotector W/System: ClassLoader referenced unknown path: /data/app/com.example.user.eyeprotector-1/lib/arm
08-16 11:09:43.598 25035-25035/com.example.user.eyeprotector D/PhoneWindowEx: [PWEx][generateLayout] setLGNavigationBarColor : colors=0xff000000
08-16 11:09:43.598 25035-25035/com.example.user.eyeprotector I/PhoneWindow: [setLGNavigationBarColor] color=0x ff000000
08-16 11:09:43.641 25035-25035/com.example.user.eyeprotector W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
08-16 11:09:43.743 25035-25035/com.example.user.eyeprotector W/ResourceType: For resource 0x7f060073, entry index(115) is beyond type entryCount(22)
08-16 11:09:43.743 25035-25035/com.example.user.eyeprotector W/ResourceType: Failure getting entry for 0x7f060073 (t=5 e=115) (error -75)
08-16 11:09:43.743 25035-25035/com.example.user.eyeprotector D/AndroidRuntime: Shutting down VM
08-16 11:09:43.744 25035-25035/com.example.user.eyeprotector E/AndroidRuntime: FATAL EXCEPTION: main
                                                                               Process: com.example.user.eyeprotector, PID: 25035
                                                                               java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.eyeprotector/com.example.user.eyeprotector.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x7f060073
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
                                                                                   at android.app.ActivityThread.access0(ActivityThread.java:157)
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                   at android.os.Looper.loop(Looper.java:148)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5530)
                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
                                                                                Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x7f060073
                                                                                   at android.content.res.Resources.getText(Resources.java:327)
                                                                                   at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
                                                                                   at android.widget.TextView.setText(TextView.java:4642)
                                                                                   at com.example.user.eyeprotector.MainActivity.onCreate(MainActivity.java:58)
                                                                                   at android.app.Activity.performCreate(Activity.java:6272)
                                                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494) 
                                                                                   at android.app.ActivityThread.access0(ActivityThread.java:157) 
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356) 
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                   at android.os.Looper.loop(Looper.java:148) 
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5530) 

检查您 R.java 字符串资源 ID 为 #0x7f060073 的文件。可能它不在那里。添加后清理并重建项目。崩溃不是由于运行时异常

错误在 MainActivity 的第 58 行,这是一个简单的错误 附上您的代码以使用 String.valueof() 方法, 这将纠正 Resources$NotFoundE‌ xception

改变

prog.setText(R.string.current_brightness + progress);

prog.setText(getResources().getString(R.string.current_brightness) + progress);

还要检查 current_brightness 字符串资源的拼写和可用性。

问题出现在行中 prog.setText(R.string.current_brightness + progress); prog.setText(R.string.current_brightness + Brightness);

setText(int) (docs) 方法与字符串资源 ID 一起使用,例如您在示例中使用的 R.string.current_brightness。此资源 ID 是一个整数,表示字符串在生成的文件中的位置。

通过尝试将字符串连接在一起,您实际上已经完成了整数加法,并且 setText(int) 方法正在查找字符串 ID 处的资源 plus当前进度。

你需要在这里使用的是格式化字符串,例如:

<string name="current_brightness">Current: %1$d</string>

然后解析字符串并使用 setText(CharSequence, Object... formatArgs) (docs) 按以下方式对其进行格式化:

prog.setText(getString(R.string.current_brightness, progress));

或者,您根本不必修改字符串,可以使用以下内容:

prog.setText(getString(R.string.current_brightness) + progress);

如果找不到 getString(),请将其替换为 getResources().getString()

In your onRequestPermissionsResult add case 0,because when user will Allow permission requestCode will be 0.

 @ Override
          public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
              switch (requestCode) {
                  case 0:
                      if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                          pergranted = true;

                      }
              }

使用这个

prog.setText(getReso‌​urces().getString(R.s‌​tring.current_brightn‌​ess)+" " + String.valueOf(Brigh‌​tness));