Blankj / AndroidUtilCode

:fire: Android developers should collect the following utils(updating).
https://blankj.com/2016/07/31/android-utils-code/
Apache License 2.0
33.3k stars 10.68k forks source link

[BUG] #1215

Closed AhmerAfzal1 closed 4 years ago

AhmerAfzal1 commented 4 years ago

Describe the bug

A clear and concise description of what the bug is.

The code of bug

Before I use this code

private static final int MULTIPLE_PERMISSIONS = 1;
    private static final String[] permissions = new String[]{
            Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE
    };

private void permissions() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) +
                    ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {                
                new RunProgram(this).execute();
            } else {                
                if (checkPermissions()) {
                    new RunProgram(this).execute();
                }
            }
        }

private boolean checkPermissions() {
           int result;
           List<String> listPermissionsNeeded = new ArrayList<>();
           for (String p : permissions) {
               result = ContextCompat.checkSelfPermission(getApplication(), p);
               if (result != PackageManager.PERMISSION_GRANTED) {
                   listPermissionsNeeded.add(p);
               }
           }
           if (!listPermissionsNeeded.isEmpty()) {
               ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[0]), MULTIPLE_PERMISSIONS);
               return false;
           }
           return true;
       }

       @Override
       public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissionsList, @NonNull int[] grantResults) {
           if (requestCode == MULTIPLE_PERMISSIONS) {
               if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                   new RunProgram(this).execute();
               } else {
                   StringBuilder permissionsDenied = new StringBuilder();
                   for (String per : permissionsList) {
                       if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
                           permissionsDenied.append("\n").append(per);
                       }
                   }
                   ToastUtils.showLong("Please allow the permission for app works properly");
               }
               return;
           }
           super.onRequestPermissionsResult(requestCode, permissions, grantResults);
       }

After using your PermissionUtils:

private void permissions() {
        PermissionUtils.permission(PermissionConstants.STORAGE).rationale(new PermissionUtils.OnRationaleListener() {
            @Override
            public void rationale(UtilsTransActivity activity, ShouldRequest shouldRequest) {
                shouldRequest.again(true);               
            }
        }).callback(new PermissionUtils.FullCallback() {
            @Override
            public void onGranted(List<String> permissionsGranted) {
                new RunProgram(SplashActivity.this).execute();
            }

            @Override
            public void onDenied(List<String> permissionsDeniedForever, List<String> permissionsDenied) {
                if (!permissionsDenied.isEmpty() || !permissionsDeniedForever.isEmpty()) {
                    finish();
                }
            }
        }).theme(new PermissionUtils.ThemeCallback() {
            @Override
            public void onActivityCreate(Activity activity) {
                ScreenUtils.setFullScreen(activity);
            }
        }).request();

And Manifest:

<activity android:name=".activity.SplashActivity"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

The stack of crash

I'm facing this issue, I twice checked every things but no effect

--------- beginning of crash
2020-04-06 14:03:00.905 6734-6734/com.ahmer.whatsapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.ahmer.whatsapp, PID: 6734
    java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:842)
        at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:805)
        at androidx.appcompat.app.AppCompatDelegateImpl.onPostCreate(AppCompatDelegateImpl.java:528)
        at androidx.appcompat.app.AppCompatActivity.onPostCreate(AppCompatActivity.java:127)
        at android.app.Instrumentation.callActivityOnPostCreate(Instrumentation.java:1373)
        at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3321)
        at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
        at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

Please help

Blankj commented 4 years ago

Try to use the theme of Theme.AppCompat.Light.NoActionBar.

AhmerAfzal1 commented 4 years ago

Thank you very much