我试图在我的应用程序中实现首选项片段,所以我设置了preferences.xml文件,应该显示首选项的片段以及交易片段。当我尝试显示SettingsFragment时,应用程序崩溃并显示以下错误日志:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
                                                              at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:431)
                                                              at android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:481)
                                                              at android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:472)
                                                              at com.ikurek.pwr.MainActivity.onNavigationItemSelected(MainActivity.java:145)
                                                              at android.support.design.widget.NavigationView$1.onMenuItemSelected(NavigationView.java:153)
                                                              at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:810)
                                                              at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
                                                              at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:957)
                                                              at android.support.design.internal.NavigationMenuPresenter$1.onClick(NavigationMenuPresenter.java:328)
                                                              at android.view.View.performClick(View.java:4780)
                                                              at android.view.View$PerformClick.run(View.java:19866)
                                                              at android.os.Handler.handleCallback(Handler.java:739)
                                                              at android.os.Handler.dispatchMessage(Handler.java:95)
                                                              at android.os.Looper.loop(Looper.java:135)
                                                              at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                              at java.lang.reflect.Method.invoke(Method.java:372)
                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)


为什么此对象为null?我还应该如何设置设置片段?我知道解决此问题的方法可能非常简单,但是我无法以某种方式自己解决这个问题。这是我的代码:

从MainActivity调用片段事务的部分:

public boolean onNavigationItemSelected(MenuItem item) {

    Fragment fragment = null;
    Class fragmentClass = null;
    int id = item.getItemId();

    if (id == R.id.nav_news) {
        fragmentClass = NewsFragment.class;

    } else if (id == R.id.nav_map) {
        fragmentClass = CatFragment.class;

    } else if (id == R.id.nav_buildings) {
        fragmentClass = BuildingsFragment.class;

    } else if (id == R.id.nav_settings) {
       fragmentClass = SettingsFragment.class;


    } else if (id == R.id.nav_info) {
        fragmentClass = AppInfoFragment.class;


    } else if (id == R.id.nav_bugreport) {
        fragmentClass = ContactFragment.class;

    }


    try {
        fragment = (Fragment) fragmentClass.newInstance();
    } catch (Exception e) {
        e.printStackTrace();
    }


    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.frameLayoutForFragments, fragment).commit();

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


我的preferences.xml:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
    android:defaultValue="false"
    android:key="saveLastPosition"
    android:summary="1st check box"
    android:title="Check"/>
<CheckBoxPreference
    android:defaultValue="true"
    android:key="keepScreenOn"
    android:summary="2nd check box"
    android:title="Box"/>
</PreferenceScreen>


和我的SettingsFragment:

public class SettingsFragment extends PreferenceFragment {

public SettingsFragment() {
    // Required empty public constructor
}


public static SettingsFragment newInstance() {
    SettingsFragment fragment = new SettingsFragment();

    return fragment;
}

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


}

最佳答案

好吧,据我所知,您正在将一个空片段传递给片段管理器。您会注意到:

Fragment fragment = null;
Class fragmentClass = SettingsFragment.class;
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frameLayoutForFragments, fragment).commit();


你有:

Fragment fragment = null;


您实际上从未将其声明为SettingsFragment。您应该具有:

Fragment fragment = new SettingsFragment();


或在您的replace语句中声明。

Class fragmentClass = SettingsFragment.class;
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frameLayoutForFragments, new SettingsFragment()).commit();

10-08 12:30