Android Fragment导致NPE“尝试调用虚方法”错误

时间:2017-01-08 20:35:40

标签: android android-fragments fragmentmanager

我想在片段中添加按钮以调用另一个片段。我添加了以下代码。

package com.munz.lop;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    public HomeFragment() {


        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction ();
        HomeFragment homeFragment = new HomeFragment ();


        transaction.add (R.id.relativelayout_for_fragment, homeFragment  );
        transaction.commit ();
        // Required empty public constructor
    }

    public void onSelectFragment(View view){

        Fragment newFragment;

        if(view == view.findViewById (R.id.btnAbout)){
            newFragment = new AboutFragment ();

        } else if(view == view.findViewById (R.id.btnFeedback)){
            newFragment = new FeedbackFragment ();


        } else if (view == view.findViewById (R.id.btnContacts)){
            newFragment = new ContactUsFragment ();



    } else {
            newFragment = new HomeFragment ();

        }

        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace ( R.id.relativelayout_for_fragment, newFragment );
        transaction.addToBackStack ( null );
        transaction.commit ();
    }





}

它没有错误,但我在Slider菜单中打开此菜单它强制使用下面的logcat停止应用程序:

D/ViewRootImpl: ViewPostImeInputStage processPointer 0
    D/ViewRootImpl: ViewPostImeInputStage processPointer 1
    D/ViewRootImpl: ViewPostImeInputStage processPointer 0
    D/ViewRootImpl: ViewPostImeInputStage processPointer 1
    D/ViewRootImpl: ViewPostImeInputStage processPointer 0
    D/ViewRootImpl: ViewPostImeInputStage processPointer 1
    D/AndroidRuntime: Shutting down VM
    E/AndroidRuntime: FATAL EXCEPTION: main
                      Process: com.munz.lop, PID: 27402
                      java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.app.FragmentTransaction android.support.v4.app.FragmentManager.beginTransaction()' on a null object reference
                          at com.munz.lop.HomeFragment.<init>(HomeFragment.java:32)
                          at com.munz.lop.SliderActivity.onNavigationItemSelected(SliderActivity.java:100)
                          at android.support.design.widget.NavigationView$1.onMenuItemSelected(NavigationView.java:150)
                          at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
                          at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
                          at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
                          at android.support.design.internal.NavigationMenuPresenter$1.onClick(NavigationMenuPresenter.java:308)
                          at android.view.View.performClick(View.java:5716)
                          at android.view.View$PerformClick.run(View.java:22596)
                          at android.os.Handler.handleCallback(Handler.java:739)
                          at android.os.Handler.dispatchMessage(Handler.java:95)
                          at android.os.Looper.loop(Looper.java:148)
                          at android.app.ActivityThread.main(ActivityThread.java:7325)
                          at java.lang.reflect.Method.invoke(Native Method)
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
    Application terminated.

1 个答案:

答案 0 :(得分:1)

你的片段的无参构造函数应该是空的;摆脱那里的一切。您应该创建片段并在处理菜单项的位置使用FragmentTransaction,在片段的构造函数中单击 not

void onNavigationItemSelected() {
    HomeFragment fragment = new HomeFragment();
    getFragmentManager().beginTransaction()
            .add(R.id.relativelayout_for_fragment, fragment)
            .commit();
}