尝试在空对象引用getDefaultsharedpreferences上调用虚拟方法

我在android上写测验,但出现错误,仍然找不到它出现的原因。错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.quizam, PID: 20399
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.quizam/com.example.quizam.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.quizam.FirstFragment.updateGuessRows(android.content.SharedPreferences)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.quizam.FirstFragment.updateGuessRows(android.content.SharedPreferences)' on a null object reference
        at com.example.quizam.MainActivity.onStart(MainActivity.java:63)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1334)
        at android.app.Activity.performStart(Activity.java:7029)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2741)

MainActivity代码:


public class MainActivity extends AppCompatActivity {

    public static final String CHOICES = "pref_numberOfChoices";
    public static final String CATEGORIES = "pref_categoriesToInclude";

    private boolean phoneDevice = true;

    private boolean preferencesChanged = true;
    SharedPreferences preferences;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        androidx.preference.PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
        androidx.preference.PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(preferenceChangeListener);
        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        int screenSize = getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;

        if ((screenSize == Configuration.SCREENLAYOUT_SIZE_LARGE) || (screenSize == Configuration.SCREENLAYOUT_SIZE_XLARGE))
        {
            phoneDevice = false;
        }

         if(phoneDevice) {
           setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
        }
    }

    @Override
    protected void onStart()
    {
        super.onStart();
        if(preferencesChanged)
        {
            FirstFragment quizFragment = (FirstFragment) getSupportFragmentManager().findFragmentById(R.id.quizFragment);
  !!!here error apears          quizFragment.updateGuessRows(PreferenceManager.getDefaultSharedPreferences(this));
            quizFragment.updateCategories(PreferenceManager.getDefaultSharedPreferences(this));
            quizFragment.resetQuiz();
            preferencesChanged = false;
        }
    }

updateGuessRows代码:

 public void updateGuessRows(SharedPreferences sharedPreferences) {
        String choices = sharedPreferences.getString(MainActivity.CHOICES, null);

        guessRows = Integer.parseInt(choices) / 2;

        for (LinearLayout layout : guessLinearLayouts) {
            layout.setVisibility(View.GONE);
        }

        for (int row = 0; row < guessRows; row++)
        {
            guessLinearLayouts[row].setVisibility(View.VISIBLE);
        }
    }

尽管正确初始化了共享首选项,但错误仍然存​​在并且应用程序仍然崩溃。我用2016年教程制作了这个应用程序,但也许在当前的Android Studio中存在一些我不理解的差异。我可以在此代码中进行哪些更改?

评论