簡體   English   中英

請求許可時我的應用程序崩潰

[英]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.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) 

檢查具有字符串資源ID#0x7f060073的R.java文件。 可能不存在。 添加后清理並重建項目。 崩潰不是由於運行時異常

錯誤在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字符串資源的拼寫和可用性。

在您的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));

問題出現在prog.setText(R.string.current_brightness + progress); prog.setText(R.string.current_brightness + Brightness);

setText(int)docs )方法與String資源ID一起使用,例如您在示例中使用的R.string.current_brightness 此資源ID是一個整數,它表示字符串在生成的文件中的位置。

通過嘗試將字符串連接在一起,您實際上已經完成了整數加法運算,並且setText(int)方法正在以字符串ID 加上當前進度查找資源。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM