黑暗模式实现在真实设备上崩溃,但在虚拟设备上崩溃

时间:2020-02-03 17:24:49

标签: android android-theme android-dark-theme

我在列表首选项内将主题更改定义为(在SettingsActivity中夸大了):

 <ListPreference
        app:defaultValue="default"
        app:entries="@array/themes_labels"
        app:entryValues="@array/themes_color"
        app:key="@string/Theme"
        app:useSimpleSumm"theme"
        app:title=aryProvider="true" />
  </PreferenceCategory>

和arraus定义为:

<resources>
  <array name="themes_labels">
    <item>"Default"</item>
    <item>"Light"</item>
    <item>"Dark"</item>
  </array>

  <string-array name="themes_color">
    <item>"Default"</item>
    <item>"Light"</item>
    <item>"Dark"</item>
  </string-array>
</resources>

现在,尽管我尝试遵循dark theme developer guide,但我不了解configuration.uiMode之类的变量,因此我已经实现为(在MainActivity中):

@Override
  protected void onCreate(Bundle savedInstanceState) {
    if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.Q) {
      AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
    } else{
      AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
    }
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    String theme = sharedPref.getString("theme", "Default");
//    Toast.makeText(this, theme, Toast.LENGTH_LONG).show();
    if (theme.equals("Dark")){
      AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    }else if (theme.equals("Light")){
      AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    } else{
      if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.Q) {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
      } else{
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
      }
    }
    super.onCreate(savedInstanceState);
    mContext = getApplicationContext();
    Bundle bundle = new Bundle();

虽然在模拟器上可以正常工作,但在我的真实设备中却突然崩溃。 但是,由于我没有办法找出问题所在,因此我没有任何线索。

有人可以帮忙吗?

0 个答案:

没有答案