Простой крах Android-проекта из-за нарушения режима StrictMode ThreadPolicy, строгого режима. DiskReadViolation - PullRequest
0 голосов
/ 07 октября 2019

Я создал новый проект Android, пустой проект-занятие и использовал StrictMode, но приложение каждый раз вылетает из-за строгого режима.DiskReadViolation

Я отвечаю на мой похожий вопрос, заданный здесь Чтение диска StrictModeнарушение setContentView для пустого действия, где решение запускается мгновенно, но я использую Android studio 3.5.1, а мгновенный запуск заменен на «Применить изменения».

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (BuildConfig.DEBUG) {
            StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                    .detectDiskReads()
                    .detectDiskWrites()
                    .detectNetwork()
                    .penaltyLog()
                    .penaltyDeath()
                    .build());
            StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                    .detectLeakedSqlLiteObjects()
                    .detectLeakedClosableObjects()
                    .penaltyLog()
                    .penaltyDeath()
                    .build());
        }
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
    }
}

Это мой logcat, вызванный: android.os.strictmode.DiskReadViolation

2019-10-08 00:05:20.821 16758-16758/com.example.myapplication D/StrictMode: StrictMode policy violation; ~duration=137 ms: android.os.strictmode.DiskReadViolation
    at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1504)
    at java.io.UnixFileSystem.checkAccess(UnixFileSystem.java:251)
    at java.io.File.exists(File.java:820)
    at android.app.ContextImpl.getDataDir(ContextImpl.java:2375)
    at android.content.ContextWrapper.getDataDir(ContextWrapper.java:245)
    at android.content.ContextWrapper.getDataDir(ContextWrapper.java:245)
    at com.oppo.embryo.EmbryoApp.isDataAccessable(EmbryoApp.java:98)
    at com.oppo.embryo.EmbryoApp.checkDecorLayout(EmbryoApp.java:122)
    at com.android.internal.policy.DecorView.onResourcesLoaded(DecorView.java:2224)
    at com.android.internal.policy.PhoneWindow.generateLayout(PhoneWindow.java:2770)
    at com.android.internal.policy.PhoneWindow.installDecor(PhoneWindow.java:2877)
    at com.android.internal.policy.PhoneWindow.getDecorView(PhoneWindow.java:2142)
    at android.app.ActivityTransitionState.setEnterActivityOptions(ActivityTransitionState.java:160)
    at android.app.Activity.performCreate(Activity.java:7323)
    at android.app.Activity.performCreate(Activity.java:7306)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3118)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3289)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2012)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:227)
    at android.app.ActivityThread.main(ActivityThread.java:7199)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:575)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:903)

2019-10-08 00:05:20.821 16758-16758/com.example.myapplication D/AndroidRuntime: Shutting down VM

2019-10-08 00:05:20.825 16758-16758/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 16758
java.lang.RuntimeException: StrictMode ThreadPolicy violation
    at android.os.StrictMode$AndroidBlockGuardPolicy.onThreadPolicyViolation(StrictMode.java:1705)
    at android.os.StrictMode$AndroidBlockGuardPolicy.lambda$handleViolationWithTimingAttempt$0(StrictMode.java:1619)
    at android.os.-$$Lambda$StrictMode$AndroidBlockGuardPolicy$9nBulCQKaMajrWr41SB7f7YRT1I.run(Unknown Source:6)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:227)
    at android.app.ActivityThread.main(ActivityThread.java:7199)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:575)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:903)
 Caused by: android.os.strictmode.DiskReadViolation
    at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1504)
    at java.io.UnixFileSystem.checkAccess(UnixFileSystem.java:251)
    at java.io.File.exists(File.java:820)
    at android.app.ContextImpl.getDataDir(ContextImpl.java:2375)
    at android.content.ContextWrapper.getDataDir(ContextWrapper.java:245)
    at android.content.ContextWrapper.getDataDir(ContextWrapper.java:245)
    at com.oppo.embryo.EmbryoApp.isDataAccessable(EmbryoApp.java:98)
    at com.oppo.embryo.EmbryoApp.checkDecorLayout(EmbryoApp.java:122)
    at com.android.internal.policy.DecorView.onResourcesLoaded(DecorView.java:2224)
    at com.android.internal.policy.PhoneWindow.generateLayout(PhoneWindow.java:2770)
    at com.android.internal.policy.PhoneWindow.installDecor(PhoneWindow.java:2877)
    at com.android.internal.policy.PhoneWindow.getDecorView(PhoneWindow.java:2142)
    at android.app.ActivityTransitionState.setEnterActivityOptions(ActivityTransitionState.java:160)
    at android.app.Activity.performCreate(Activity.java:7323)
    at android.app.Activity.performCreate(Activity.java:7306)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3118)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3289)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2012)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:227) 
    at android.app.ActivityThread.main(ActivityThread.java:7199) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:575) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:903)
...