简体   繁体   中英

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. 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?

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.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. 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.

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));

The issue occurs in the lines prog.setText(R.string.current_brightness + progress); and 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 . This resource id is an integer that refers to the location of the string in the generated files.

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.

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:

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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