1

こんにちは、(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;

                  }
          }


      }

  }

ログキャット

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

6 に答える 6

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;

                      }
              }
于 2016-08-16T10:11:18.717 に答える
0

String リソース ID #0x7f060073 の R.java ファイルを確認してください。おそらくそれはありません。それを追加した後、プロジェクトをクリーンアップして再構築します。クラッシュはランタイム例外によるものではありません

于 2016-08-16T08:27:32.697 に答える
0

エラーは MainActivity の 58 行目にあります。これはString.valueof()メソッドを使用するコードを囲む単純なエラーです。これにより、Resources$NotFoundE‌ 例外が修正されます。

于 2016-08-16T08:27:40.460 に答える
0

変化する

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

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

current_brightnessまた、文字列リソースのスペルと可用性も確認してください。

于 2016-08-16T08:27:44.723 に答える
-1

問題は行で発生し prog.setText(R.string.current_brightness + progress); prog.setText(R.string.current_brightness + Brightness);

( docssetText(int) ) メソッドは、例で使用したような文字列リソース ID で使用されます。このリソース ID は、生成されたファイル内の文字列の場所を参照する整数です。R.string.current_brightness

文字列を連結しようとすると、実際に整数の加算が行われ、メソッドは文字列の ID現在の進行状況setText(int)でリソースを探します。

ここで使用する必要があるのは、次のような書式設定された文字列です。

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

于 2016-08-16T09:00:44.963 に答える