使用共享首选项时无法在第一次运行时打开警告框

问题描述 投票:0回答:0

你好我试图在应用程序第一次启动时添加一个警告框,我已经完成了这个post但似乎对我不起作用,它给了我一个错误

错误在这里

E/libc: Access denied finding property "ro.vendor.df.effect.conflict"
E/Perf: Fail to get file list com.example.memeart
    getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
E/Perf: Fail to get file list com.example.memeart
    getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.memeart, PID: 31983
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.memeart/com.example.memeart.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3232)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3457)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2044)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:224)
        at android.app.ActivityThread.main(ActivityThread.java:7562)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
        at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:188)
        at com.example.memeart.MainActivity.<init>(MainActivity.java:26)
        at java.lang.Class.newInstance(Native Method)
        at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
        at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1251)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3220)

这是我已经实施的

SharedPreferences prefs = getSharedPreferences("MyPrefs", MODE_PRIVATE);
    boolean isFirstLaunch = prefs.getBoolean("is_first_launch", true);

  if (isFirstLaunch) {
                // Show alert dialog
                AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.CustomAlertDialogStyle);
                builder.setTitle(R.string.info_card_message)
                        .setInverseBackgroundForced(Boolean.parseBoolean("@color/cardview_dark_background"))
                        .setPositiveButton(R.string.info_card_ok_button, null)
                        .show();
                // Set "is_first_launch" to false
                SharedPreferences.Editor editor = prefs.edit();
                editor.putBoolean("is_first_launch", false);
                editor.apply();
            }
android boolean sharedpreferences
© www.soinside.com 2019 - 2024. All rights reserved.