gpt4 book ai didi

java - PreferenceFragmentCompat 不显示

转载 作者:太空宇宙 更新时间:2023-11-04 11:28:32 26 4
gpt4 key购买 nike

我需要帮助来解决 PreferenceFragmentCompat 的问题,该问题根本不显示,控制台中没有错误,没有消息,什么也没有,只是一个空白页面。

该 fragment 显示为在抽屉导航中选择元素的结果。这是 Activity XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false">

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
android:id="@+id/list_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginBottom="24dp"
android:layout_marginEnd="24dp"
app:elevation="12dp"
app:fabSize="normal"
app:rippleColor="@color/nowControlsNormal"
app:srcCompat="@android:drawable/ic_popup_sync" />

</FrameLayout>

<android.support.design.widget.NavigationView
android:id="@+id/drawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/drawer_top"
app:itemIconTint="@color/nowBlack"
app:itemTextColor="@color/nowBlack"
app:menu="@menu/drawer_menu" />

</android.support.v4.widget.DrawerLayout>

这是 Java Activity :

public class HomeActivity extends AppCompatActivity {

SharedPreferences settings;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
NavigationView nav;
TextView drawerUsername;
FloatingActionButton fab;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
settings = getSharedPreferences("NowCLOUD", 0);
setContentView(R.layout.activity_home);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
nav = (NavigationView) findViewById(R.id.drawer);
fab = (FloatingActionButton) findViewById(R.id.fab);
View headerView = nav.getHeaderView(0);
drawerUsername = (TextView) headerView.findViewById(R.id.drawer_username);

fab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//TODO: Gestire aggiornamento al click della FAB
}
});

getSupportActionBar().setTitle("Home");

drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_open, R.string.drawer_close) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};

drawerLayout.setDrawerListener(drawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);

getSupportFragmentManager().beginTransaction()
.add(R.id.list_container, HomeFragment.newInstance())
.commit();

drawerUsername.setText(settings.getString("username", "Error"));

nav.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
drawerLayout.closeDrawers();
displayView(menuItem.getItemId());
return true;
}
});
}

public void displayView(int viewId) {

Fragment fragment = null;
String title = getString(R.string.app_name);

switch (viewId) {
case R.id.drawer_home:
fragment = HomeFragment.newInstance();
title = "Home";
break;

case R.id.drawer_settings:
fragment = SettingsFragment.newInstance();
title = "Settings";
break;
}

if (fragment != null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.list_container, fragment)
.commit();
}

// set the toolbar title
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle(title);
}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);

}
}

这是 fragment 的 XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.preference.PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">

<android.support.v7.preference.PreferenceCategory
android:title="@string/settings_synchronization">

<android.support.v7.preference.SwitchPreferenceCompat
android:key="auto_updates"
android:title="@string/settings_auto_sync" />

</android.support.v7.preference.PreferenceCategory>

</android.support.v7.preference.PreferenceScreen>

这是 fragment 的 Java:

public class SettingsFragment extends PreferenceFragmentCompat {
View rootView;

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.fragment_settings);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
//rootView = inflater.inflate(R.xml.fragment_settings,container,false);
return rootView;
}

@Override
public PreferenceFragmentCompat getCallbackFragment() {
return this;
}

public static SettingsFragment newInstance() {
Bundle args = new Bundle();
SettingsFragment fragment = new SettingsFragment();
fragment.setArguments(args);
return fragment;
}
}

我已经删除了所有对此目的无用的代码。抱歉,代码量很大。

最佳答案

我自己想出来了:
当您使用preferencefragment时,您不能覆盖onCreateView方法,否则您将得到一个空白屏幕。

关于java - PreferenceFragmentCompat 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44078164/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com