单击设置时,首选项片段无法打开

时间:2015-02-12 10:27:38

标签: android

我正在研究一个简单的偏好片段示例...要显示偏好片段......但它没有显示偏好片段设置n标题栏...它给出一个空白!这就是它显示的内容并且没有显示偏好片段,只是提到了标题pref片段。

MainActivity.java:

package com.example.preferencefragments;

import android.annotation.SuppressLint;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


@SuppressLint("NewApi") public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        PrefFragment prefFragment = new PrefFragment();
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(android.R.id.content, prefFragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

PerfFragment.java:

package com.example.preferencefragments;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.preference.PreferenceFragment;

@SuppressLint("NewApi") public class PrefFragment extends PreferenceFragment
{

    @SuppressLint("NewApi") @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.layout.preferences);
    }

}

activitymain.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.preferencefragments.MainActivity" >

    <Button
        android:id="@+id/setpreference"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Set Preference" 
        />

    <TextView 
        android:id="@+id/setting_checkbox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
</LinearLayout>

的preferences.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory
        android:title="PreferenceCategory A">

        <CheckBoxPreference
            android:key="PREF_CHECKBOX"
            android:title="Title"
            android:summary="Summary" />


    </PreferenceCategory>


</PreferenceScreen>

这是图片: enter link description here

1 个答案:

答案 0 :(得分:0)

我的猜测:您应该使用getSupportFragmentManager()而不是getFragmentManager(),因为您似乎正在使用appcompat-v7支持库。