简体   繁体   English

请求许可时我的应用程序崩溃

[英]My app crashes while requesting permission

Hi im making an app for controlling device screen which needs (WRITE_SETTINGS) permission i used a method to request permission but the apps crashes. 嗨,我正在制作一个用于控制需要(WRITE_SETTINGS)权限的设备屏幕的应用程序,我使用了一种请求权限的方法,但应用程序崩溃了。 my apps checks if the version the higher than android 5 or not of higher it calls a method to request permission but it wont work, any help? 我的应用程序检查该版本是否高于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 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.access$900(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.access$900(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.access$900(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.access$900(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.access$900(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.access$900(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) 

Check you R.java file with String resource ID #0x7f060073. 检查具有字符串资源ID#0x7f060073的R.java文件。 Probably it is not there. 可能不存在。 Clean and Rebuild the project after adding that. 添加后清理并重建项目。 The crash is not due to Runtime exception 崩溃不是由于运行时异常

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

Change 更改

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

to

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

And check the spelling and availability of current_brightness string resource too. 并且还要检查current_brightness字符串资源的拼写和可用性。

In your onRequestPermissionsResult add case 0,because when user will Allow permission requestCode will be 0. 在您的onRequestPermissionsResult中添加大小写0,因为用户何时将允许权限requestCode将为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));

The issue occurs in the lines prog.setText(R.string.current_brightness + progress); 问题出现在prog.setText(R.string.current_brightness + progress); and prog.setText(R.string.current_brightness + Brightness); prog.setText(R.string.current_brightness + Brightness);

The setText(int) ( docs ) method is used with String resource id's such as the one you use in the example, R.string.current_brightness . setText(int)docs )方法与String资源ID一起使用,例如您在示例中使用的R.string.current_brightness This resource id is an integer that refers to the location of the string in the generated files. 此资源ID是一个整数,它表示字符串在生成的文件中的位置。

By attempting to concatenate the strings together, you have actually done integer addition and the setText(int) method is looking for a resource at an id of your string plus the current progress. 通过尝试将字符串连接在一起,您实际上已经完成了整数加法运算,并且setText(int)方法正在以字符串ID 加上当前进度查找资源。

What you need to use here is either a formatted string such as the following: 您在这里需要使用的是格式化字符串,例如:

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

and then resolve the string and format it using setText(CharSequence, Object... formatArgs) ( docs ) in the following way: 然后解析字符串并使用setText(CharSequence, Object... formatArgs)docs )通过以下方式对其进行格式化:

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

Alternatively, you don't have to modify you string at all, and can use the following: 另外,您根本不需要修改字符串,可以使用以下命令:

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

If getString() cannot be found, replace this with getResources().getString() 如果找不到getString()则将其替换为getResources().getString()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM