尝试通过接口传递布尔值时获取空指针异常

时间:2019-06-14 12:42:41

标签: android nullpointerexception

我正在使用接口,这是标准(最佳?)实践,用于在一系列片段之间进行通信。业务逻辑要求应用程序在片段n + 1中收集一些信息,如果轻按“下一步”按钮,则用户将转到片段n + 2。如果轻按“后退”按钮,则用户转到片段n。我还使用了不错的滑动动画来显示从一个片段到另一个片段的过渡,具体取决于方向。我无法弄清楚为什么这行不通,并且在此行上收到空指针错误:          createPlanListener.onCreatePlan(bundle);

这是我触发转换的初始片段 Mealplan.class 。我保留了Android Studio生成的所有样板代码,如下所示:

public class MealplanFragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    private FloatingActionButton createMealplan;
    // bunch of variables
    private Bundle bundle;

    private OnCreatePlanListener createPlanListener;

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

    public static MealplanFragment newInstance(String param1, String param2) {
        MealplanFragment fragment = new MealplanFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Other code that has nothing to do with the bundle or the listener


        // Floating action bar
        createMealplan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bundle.putBoolean("direction", true);
                createPlanListener.onCreatePlan(bundle);
            }
        });

        return mealplanView;
    }

    public void onButtonPressed(Bundle bundle) {
        if (createPlanListener != null) {
            createPlanListener.onCreatePlan(bundle);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mealplanContext = context;
        if (context instanceof OnCreatePlanListener) {
            createPlanListener = (OnCreatePlanListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        createPlanListener = null;
    }

    public interface OnCreatePlanListener {
        void onCreatePlan(Bundle bundle);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

这是 MainActivity.class

public class MainActivity extends AppCompatActivity implements
        MealplanFragment.OnCreatePlanListener {

   // Non related variables


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       // MealplanFragment is the default fragment at onCreate
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, new MealplanFragment(), null).commit();
        }
    }

    @Override
    public void onCreatePlan(Bundle bundle) {
        if (bundle != null) {
            Boolean direction = bundle.getBoolean("direction");
            ReceptionFragment fragment = new ReceptionFragment();
            openFragment(bundle, fragment, direction);
        }
    }


    private void openFragment(Bundle bundle, Fragment fragment, Boolean direction) {

        fragment.setArguments(bundle);

        //Starting fragment with animation
        if (direction) {
            android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_right).replace(R.id.frame_container, fragment, null);
            fragmentTransaction.commit();
        } else {
            android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.enter_from_left, R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_left).replace(R.id.frame_container, fragment, null);
            fragmentTransaction.commit();
        }
    }



}

1 个答案:

答案 0 :(得分:0)

createMealplan.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            bundle.putBoolean("direction", true);
            createPlanListener.onCreatePlan(bundle);
        }
    });

这是您的点击监听器。 bundle被定义为类变量,但从未初始化,因此出现了空指针异常。我建议您使用局部变量->创建bundle的新实例,添加数据,然后调用回调。另外,createPlanListener是可以为空的,因此您也应该为此添加检查。