我试图将PreferenceFragment添加到底部导航栏中,但是每次应用程序加载片段时,都会引发错误。我的问题是,如何将SettingsFragment添加到现有的底部导航栏中?我应该更改填充栏的方式吗?
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.eduardo.proyectolainne, PID: 21633
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:533)
at android.support.v7.preference.PreferenceFragmentCompat.onViewCreated(PreferenceFragmentCompat.java:324)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1471)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2243)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:654)
at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:168)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1244)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:669)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:631)
at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:612)
at com.example.eduardo.proyectolainne.MainActivity$1.onTabSelected(MainActivity.java:42)
at com.aurelhubert.ahbottomnavigation.AHBottomNavigation.updateItems(AHBottomNavigation.java:590)
at com.aurelhubert.ahbottomnavigation.AHBottomNavigation.access$000(AHBottomNavigation.java:52)
at com.aurelhubert.ahbottomnavigation.AHBottomNavigation$2.onClick(AHBottomNavigation.java:437)
at android.view.View.performClick(View.java:5619)
at android.view.View$PerformClick.run(View.java:22298)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6375)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:883)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
SettingsFragment.java
package com.example.eduardo.proyectolainne;
import android.os.Bundle;
import android.support.v14.preference.PreferenceFragment;
import android.support.v7.preference.ListPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.preference_screen, rootKey);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroupcontainer, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_settings, container, false);
}
}
preference_screen.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="@string/title_first_section">
<CheckBoxPreference
android:key="checkbox_preference"
android:title="@string/title_checkbox_preference"
android:defaultValue="@string/default_checkbox_preference"/>
<EditTextPreference
android:key="edittext_preference"
android:title="@string/title_edittext_preference"
android:summary="@string/summary_edittext_preference"
android:dialogTitle="@string/dialog_title_edittext_preference"
android:dependency="checkbox_preference" />
</PreferenceCategory>
<PreferenceCategory
android:title="@string/title_second_section">
<Preference
android:title="@string/title_intent_preference">
<intent android:action="android.intent.action.VIEW"
android:data="http://codepath.com/" />
</Preference>
</PreferenceCategory>
</PreferenceScreen>
MainActivity.java
package com.example.eduardo.proyectolainne;
import android.graphics.Color;
import android.icu.text.RelativeDateTimeFormatter;
import android.os.Bundle;
import android.preference.PreferenceScreen;
import android.support.annotation.ColorRes;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
import com.aurelhubert.ahbottomnavigation.AHBottomNavigationItem;
public class MainActivity extends AppCompatActivity {
private final int[] colors = {R.color.bottomtab_0, R.color.bottomtab_1, R.color.bottomtab_2};
private NoSwipePager viewPager;
private AHBottomNavigation bottomNavigation;
private BottomBarAdapter pagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupViewPager();
bottomNavigation = (AHBottomNavigation) findViewById(R.id.bottom_navigation);
setupBottomNavBehaviors();
setupBotomNavStyle();
addBottomNavigationItems();
bottomNavigation.setCurrentItem(0);
bottomNavigation.setOnTabSelectedListener(new AHBottomNavigation.OnTabSelectedListener() {
@Override
public boolean onTabSelected(int position, boolean wasSelected) {
if (!wasSelected) {
viewPager.setCurrentItem(position);
}
int lastItemPos = bottomNavigation.getItemsCount() - 1;
return true;
}
});
}
private void setupViewPager() {
viewPager = (NoSwipePager) findViewById(R.id.viewpager);
viewPager.setPagingEnabled(false);
pagerAdapter = new BottomBarAdapter(getSupportFragmentManager());
pagerAdapter.addFragments(createHomeFragment(R.color.bottomtab_0));
pagerAdapter.addFragments(createOrdersFragment(R.color.bottomtab_1));
pagerAdapter.addFragments(createSettingsFragmet());
viewPager.setAdapter(pagerAdapter);
}
@NonNull
private HomeFragment createHomeFragment(int color) {
HomeFragment fragment = new HomeFragment();
fragment.setArguments(passFragmentArguments(fetchColor(color)));
return fragment;
}
@NonNull
private OrdersFragment createOrdersFragment(int color) {
OrdersFragment fragment = new OrdersFragment();
fragment.setArguments(passFragmentArguments(fetchColor(color)));
return fragment;
}
@NonNull
private SettingsFragment createSettingsFragmet() {
SettingsFragment fragment = new SettingsFragment();
return fragment;
}
@NonNull
private Bundle passFragmentArguments(int color) {
Bundle bundle = new Bundle();
bundle.putInt("color", color);
return bundle;
}
public void setupBottomNavBehaviors() {
bottomNavigation.setTranslucentNavigationEnabled(false);
}
private void setupBotomNavStyle() {
bottomNavigation.setDefaultBackgroundColor(Color.WHITE);
bottomNavigation.setAccentColor(fetchColor(R.color.bottomtab_0));
bottomNavigation.setInactiveColor(fetchColor(R.color.bottomtab_item_resting));
bottomNavigation.setColoredModeColors(Color.WHITE,
fetchColor(R.color.bottomtab_item_resting));
bottomNavigation.setColored(true);
bottomNavigation.setTitleState(AHBottomNavigation.TitleState.ALWAYS_SHOW);
}
private void addBottomNavigationItems() {
AHBottomNavigationItem item1 = new AHBottomNavigationItem(R.string.tab_1, R.drawable.birthday_cake_ic, colors[0]);
AHBottomNavigationItem item2 = new AHBottomNavigationItem(R.string.tab_2, R.drawable.baseline_shopping_cart_white_18dp, colors[1]);
AHBottomNavigationItem item3 = new AHBottomNavigationItem(R.string.tab_3, R.drawable.baseline_settings_white_18dp, colors[2]);
bottomNavigation.addItem(item1);
bottomNavigation.addItem(item2);
bottomNavigation.addItem(item3);
}
private int fetchColor(@ColorRes int color) {
return ContextCompat.getColor(this, color);
}
}