Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android id.list上的PreferenceFragment错误,但id.content上没有_Android_Android Fragments_Preferences - Fatal编程技术网

Android id.list上的PreferenceFragment错误,但id.content上没有

Android id.list上的PreferenceFragment错误,但id.content上没有,android,android-fragments,preferences,Android,Android Fragments,Preferences,我真正想做的是在现有布局中添加一个带有其他项目(如按钮)的首选项屏幕,而不使用不推荐的方法。我已经查看了“将按钮添加到首选项屏幕”,并使用不推荐的方法使其半工作 我有两个xml布局。首先是首选项屏幕(目前首选项类别是多余的),为了清晰起见,省略了12个其他复选框首选项: <?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/re

我真正想做的是在现有布局中添加一个带有其他项目(如按钮)的首选项屏幕,而不使用不推荐的方法。我已经查看了“将按钮添加到首选项屏幕”,并使用不推荐的方法使其半工作

我有两个xml布局。首先是首选项屏幕(目前首选项类别是多余的),为了清晰起见,省略了12个其他复选框首选项:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
    android:key="@string/specialization_prefs"
    android:title="@string/select_specializations">

    <CheckBoxPreference
        android:key="@string/pulse_ox_key"
        android:title="@string/pulse_ox"
        android:defaultValue="true" />

    <CheckBoxPreference
        android:key="@string/bp_key"
        android:title="@string/bp"
        android:defaultValue="true" />

 </PreferenceCategory>
最后,我的首选活动如下:

public class SpecializationsFragment extends PreferenceFragment
{

    @Override
    public void onAttach(Activity specializationsActivity)
    {
        super.onAttach(specializationsActivity);
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.specializations);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        return super.onCreateView(inflater, container, savedInstanceState);
    }
}
public class SpecializationsActivity extends PreferenceActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.specializations_layout);

        getFragmentManager().beginTransaction()
            .replace(android.R.id.list, new SpecializationsFragment())
            .commit();
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button
    android:id="@+id/btn_done"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Exit and Save" />

<RelativeLayout 
    android:layout_below="@+id/btn_done"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/specializations_layout" >

    <ListView android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
  </RelativeLayout>
</RelativeLayout>
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.specializations_layout);

    // Display the fragment as the main content.

    getFragmentManager().beginTransaction()
            .replace(R.id.specializations_layout, new SpecializationsFragment())
            .commit();
    findViewById(android.R.id.list).setBackgroundColor(Color.argb(0, 0, 0, 0));
}
如果我将“android.R.id.list”替换为“android.R.id.content”,它就可以正常工作了。我看到我的按钮,但我的首选项屏幕在它上面滚动。这就是我在遵循不推荐的按钮解决方案时得到的结果。如果改用“android.R.id.list”,应用程序会崩溃,并说“AdapterView中不支持addView(View)”


我也搜索过这个问题。我遗漏了什么?这段代码目前只做GUI显示。我需要添加什么才能让按钮显示,让首选项屏幕在它下面的空间中滚动,就像它在自己的“片段”中应该做的那样?我想这就是片段背后的全部想法。(是的,只要不存在首选项屏幕,它们实际上工作得很好。)使用“添加”方法和“替换”方法也没有区别。

对于任何关心的人,我找到了一个解决方案。这不是我所期望的,但它起了作用。首先,我需要在布局中嵌套一个布局。然后,我需要在按钮下方对齐这个嵌套布局。我还需要在其中放置虚拟ListView,但我不知道为什么。所以xml显示如下:

public class SpecializationsFragment extends PreferenceFragment
{

    @Override
    public void onAttach(Activity specializationsActivity)
    {
        super.onAttach(specializationsActivity);
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.specializations);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        return super.onCreateView(inflater, container, savedInstanceState);
    }
}
public class SpecializationsActivity extends PreferenceActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.specializations_layout);

        getFragmentManager().beginTransaction()
            .replace(android.R.id.list, new SpecializationsFragment())
            .commit();
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button
    android:id="@+id/btn_done"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Exit and Save" />

<RelativeLayout 
    android:layout_below="@+id/btn_done"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/specializations_layout" >

    <ListView android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
  </RelativeLayout>
</RelativeLayout>
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.specializations_layout);

    // Display the fragment as the main content.

    getFragmentManager().beginTransaction()
            .replace(R.id.specializations_layout, new SpecializationsFragment())
            .commit();
    findViewById(android.R.id.list).setBackgroundColor(Color.argb(0, 0, 0, 0));
}

我不明白这里到底发生了什么,特别是需要虚拟元素及其android:id/list属性。我希望看到它在没有ListView元素的情况下工作,因为我相信如果这是一个普通片段而不是首选屏幕,情况会是这样。我将感谢任何能够解释这一点的人对我来说!

我可能比你更不了解正在发生的事情,但我猜片段管理器想要替换/填充布局的首选项,而不是你(和我)试图做的列表元素。奇怪的是,它不适用于线性布局(我得到的只是一个空白屏幕)但是Relative成功了。我还能够使用顶级android.support.design.widget.CoordinatorLayout添加一个浮动按钮。尽管如此,您的解决方案对我来说很有效,所以您应该接受它作为答案。此外,这是迄今为止我发现的将按钮添加到首选项屏幕或片段(无论是否浮动)的最佳方法。也是唯一一个同时显示按钮和首选项屏幕的工具(我已经预先填充了几个项目,需要更动态地添加)。希望未来的搜索将引导人们访问此页面。