使用v7首选项支持库中的新PreferenceFragmentCompat:http://developer.android.com/tools/support-library/features.html#v7-preference,我收到此错误
E java.lang.IllegalStateException: Must specify preferenceTheme in theme
E at android.support.v7.preference.PreferenceFragmentCompat.onCreate(PreferenceFragmentCompat.java:202)
Run Code Online (Sandbox Code Playgroud)
应该设置什么主题?
更新:我尝试过使用
<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
正如@Bogato所建议的那样,它看起来并不正确,即使在Lollipop上也看起来非常Holo.
支持库:
原生偏好:
android android-fragments android-support-library preferencefragment preference-v7
所以我一直在尝试使用androidx.preference.PreferenceFragmentCompat创建一个设置活动,它一切正常.
但是由于某种原因,在偏好类别和偏好本身上都存在一些填充.我设法通过使用app:iconSpaceReserved ="false"来摆脱首选项的填充,但这似乎不适用于类别.
我已经尝试了所有各种主题,PreferenceThemeOverlay.v14.Material等但它们似乎没有什么区别
这是我的一切代码!
SettingsActivity.java
import android.os.Bundle;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
public class SettingsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
Toolbar toolbar = findViewById(R.id.settings_toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
}
}
}
Run Code Online (Sandbox Code Playgroud)
activity_settings.xml
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SettingsActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/settings_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
style="@style/ToolbarTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title_settings"
android:textColor="@color/font_dark_primary" />
</androidx.appcompat.widget.Toolbar>
<fragment
android:name="com.henrytwist8gmail.fullcart.SettingsTestFragment"
android:tag="settings_fragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/settings_toolbar" />
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
SettingsTestFragment.java
import android.os.Bundle; …
Run Code Online (Sandbox Code Playgroud) android android-appcompat android-preferences preferencefragment androidx
我是Android Studio的新手,也是Android编程的初学者.我看了看,我找不到合适的事情.
我想要的是安装一个库来使用PreferenceFragmentCompat或任何替换android.app.PreferenceFragment的类,这样我的应用程序就可以在API 11及更低版本中运行.
任何人都可以给我一些细节,比如我应该使用哪个库以及如何在我的AS项目中安装它.
编辑:所以我开始使用android.support.v4
和android.support.v7
库来开发这个应用程序来处理片段,所以我可以在API 11和更低版本上运行它.
这是我的MainActivity.java.
package com.example.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.ActionBar;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.widget.DrawerLayout;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/ …
Run Code Online (Sandbox Code Playgroud) 我正在尝试在我的首选项屏幕中显示操作栏.为此,我在SettingActivity中添加了以下代码
public class PreferencesActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.preferences_activity);
getFragmentManager().beginTransaction()
.replace(R.id.container, new PreferencesFragment()).commit();
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_HOME_AS_UP);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
Run Code Online (Sandbox Code Playgroud)
然后是我在PreferencesFragment中的其余代码.这样可以正常工作,但只要按下PreferenceScreen首选项,操作栏就会隐藏.如果我回到偏好主屏幕,我可以再次看到它.知道如何显示操作栏(并使用PreferenceScreen标签更新)吗?
编辑:查看PreferenceScreen代码,当单击PreferenceScreen时,它看起来像是一个全屏对话框.因为我的偏好有标题,所以Dialog也应该显示标题......但事实并非如此
Run Code Online (Sandbox Code Playgroud)// Set the title bar if title is available, else no title bar final CharSequence title = getTitle(); Dialog dialog = mDialog = new Dialog(context, context.getThemeResId()); if (TextUtils.isEmpty(title)) { dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE); } else { dialog.setTitle(title); }
android preferenceactivity android-actionbar preferencefragment
我PreferenceFragment
在ActionBarActivity
support-v7库中使用.
在我的活动中Toolbar
.一切顺利,直到我打开一个嵌套PreferenceScreen
.
在打开的屏幕Toolbar
中隐藏.
也许有人知道这个问题的解决方法?
首选项xml文件:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="Main category" >
<EditTextPreference
android:defaultValue="defaultValue"
android:key="key_global_setting"
android:title="Global title" />
</PreferenceCategory>
<PreferenceCategory android:title="Nested screens" >
<PreferenceScreen
android:persistent="false"
android:title="@string/settings_facility_title" >
<CheckBoxPreference
android:defaultValue="false"
android:key="nested_screen_1_1"
android:title="Nested screen 1.1 check box" />
<CheckBoxPreference
android:defaultValue="true"
android:key="nested_screen_1_2"
android:title="Nested screen 1.2 check box" />
</PreferenceScreen>
<PreferenceScreen
android:persistent="false"
android:title="@string/settings_menu_screen_title" >
<CheckBoxPreference
android:defaultValue="true"
android:key="nested_screen2"
android:title="Nested screen 2 check box" />
</PreferenceScreen>
</PreferenceCategory>
</PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)
活动布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout …
Run Code Online (Sandbox Code Playgroud) android android-preferences android-support-library preferencefragment android-toolbar
我有一个简单的ActionBarActivity,它包含一个PreferenceFragment,如下所示:
import android.app.Activity;
import android.os.Bundle;
import android.preference.PreferenceFragment;
public class SettingsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(R.id.fra_settings_container, new SettingsFragment()).commit();
}
public static class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的preferences.xml:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:defaultValue="15"
android:dialogTitle="@string/settings_heading_alarm_interval"
android:entries="@array/alarm_interval_entries"
android:entryValues="@array/alarm_interval_values"
android:key="alarm_interval"
android:title="@string/settings_heading_alarm_interval" />
<ListPreference
android:defaultValue="1"
android:dialogTitle="@string/settings_heading_alarm_type"
android:entries="@array/alarm_type_entries"
android:entryValues="@array/alarm_type_values"
android:key="alarm_type"
android:title="@string/settings_heading_alarm_type"
/>
<CheckBoxPreference …
Run Code Online (Sandbox Code Playgroud) 我已经实现了我的偏好,如官方指南中所示.
我有一个PreferenceActivity,可以像这样创建PreferenceFragment:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
if (extras != null)
{
Bundle bundle = new Bundle();
_widgetID = extras.getInt(GlobalSettings.EXTRA_WIDGET_ID);
bundle.putInt(GlobalSettings.EXTRA_WIDGET_ID, _widgetID);
WidgetSettingsFragment fragment = new WidgetSettingsFragment();
fragment.setArguments(bundle);
getFragmentManager().beginTransaction().replace(android.R.id.content,
fragment).commit();
}
}
Run Code Online (Sandbox Code Playgroud)
PreferenceFragment从资源加载首选项,它们包含一个首选项子屏幕,如下所示:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<!-- opens a subscreen of settings -->
<PreferenceScreen
android:key="button_voicemail_category_key"
android:title="@string/voicemail"
android:persistent="false">
<ListPreference
android:key="button_voicemail_provider_key"
android:title="@string/voicemail_provider" ... />
<!-- opens another nested subscreen -->
<PreferenceScreen
android:key="button_voicemail_setting_key"
android:title="@string/voicemail_settings"
android:persistent="false">
...
</PreferenceScreen>
<RingtonePreference
android:key="button_voicemail_ringtone_key"
android:title="@string/voicemail_ringtone_title"
android:ringtoneType="notification" ... />
...
</PreferenceScreen>
...
</PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)
到目前为止这种方法效果很好,但现在我想 …
settings android preferences preferenceactivity preferencefragment
我需要PreferenceFragmentCompat的自定义布局.在PreferenceFragmentCompat的文档中,您似乎可以在onCreateView()中膨胀并返回一个视图.
然而,NPE结果: -
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)' on a null object reference
at android.support.v7.preference.PreferenceFragmentCompat.bindPreferences(PreferenceFragmentCompat.java:511)
at android.support.v7.preference.PreferenceFragmentCompat.onActivityCreated(PreferenceFragmentCompat.java:316)
at com.cls.example.MyPrefFrag.onActivityCreated(MyPrefFrag.java:42)
Run Code Online (Sandbox Code Playgroud)
在我检查了PreferenceFragmentCompat:onCreateView的源代码后,我发现了以下代码: -
RecyclerView listView = this.onCreateRecyclerView(themedInflater, listContainer, savedInstanceState);
if(listView == null) {
throw new RuntimeException("Could not create RecyclerView");
} else {
this.mList = listView; //problem
...
return view;
}
Run Code Online (Sandbox Code Playgroud)
因此,如果覆盖onCreateView()并返回自定义布局,则不会调用onCreateRecyclerView(),并且不会设置RecyclerView私有字段mList.因此,setAdapter()上的NPE结果.
我是否应该假设自定义布局对PreferenceFragmentCompat不可行?
我Users
在另一个PreferenceScreen(调用它Main
)内部有一个内部PreferenceScreen (调用它).
当我点击Users
一个新屏幕打开,我可以在那里更改我的首选项(很多CheckBoxes).
当我关闭此屏幕并回到Main
PreferenceScreen 时,我想检测(触发回调).
我找到的唯一方法是创建一个继承PreferenceScreen和重载的新类onPrepareForRemoval
我想知道是否有更简单的方法可以做到这一点.
我正在使用 MVVM 和改造构建一个天气应用程序,我最近添加了一个 PreferencesFragmentCompat 子类来使用首选项库实现一些用户设置。这样做之后,我的应用程序将无法运行,并且我不断收到以下几行错误:
2020-04-08 00:54:12.346 18079-18079/? E/de.flogaweathe: Unknown bits set in runtime_flags: 0x8000
2020-04-08 00:54:12.410 18079-18079/com.nesoinode.flogaweather E/libc: Access denied finding property "ro.vendor.df.effect.conflict"
2020-04-08 00:54:12.421 18079-18110/com.nesoinode.flogaweather E/Perf: Fail to get file list com.nesoinode.flogaweather
2020-04-08 00:54:12.421 18079-18110/com.nesoinode.flogaweather E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
2020-04-08 00:54:12.421 18079-18110/com.nesoinode.flogaweather E/Perf: Fail to get file list oat
2020-04-08 00:54:12.422 18079-18110/com.nesoinode.flogaweather E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
Run Code Online (Sandbox Code Playgroud)
我不知道这些是什么,我在堆栈或谷歌上找不到任何具体的答案。没有迹象表明是什么导致了错误,所以我不知道我是否做错了什么是图书馆的问题。有任何想法吗?
这是我从 xml 资源文件添加首选项的 …