gpt4 book ai didi

android - 空指针异常 : FragmentManager. beginTransaction()

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:06:55 27 4
gpt4 key购买 nike

我在尝试从第一个 fragment 启动 fragment 时遇到此错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.FragmentTransaction android.app.FragmentManager.beginTransaction()' on a null object reference

这是我遇到错误的方法:

    @Override
public void onClick(View v) {
Fragment fragment = new PropertyFragment();
if (fragment != null) {
FragmentManager fragmentManager = fragment.getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
fragmentTransaction.replace(R.id.rent_viewer, fragment);
fragmentTransaction.addToBackStack(null);

// Commit the transaction
fragmentTransaction.commit();
}
}

准确地说,是以下代码指令导致了错误:

fragmentManager.beginTransaction();

这是类和嵌套类的样子:

public class RentFragment extends Fragment {    

...

@SuppressWarnings("unused")
private OnRentFragmentInteractionListener mListener;

public RentFragment() {
}

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnRentFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnRentFragmentInteractionListener");
}
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_rent, container, false);

myOnClickListener = new RentOnClickListener(getActivity());

return rootView;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}

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

public interface OnRentFragmentInteractionListener {
public void onRentFragmentInteraction(Uri uri);
}

private static class RentOnClickListener implements View.OnClickListener {

private final Context context;

private RentOnClickListener(Context context) {
this.context = context;
}

@Override
public void onClick(View v) {
Fragment fragment = new PropertyFragment();
if (fragment != null) {
FragmentManager fragmentManager = fragment.getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
fragmentTransaction.replace(R.id.rent_viewer, fragment);
fragmentTransaction.addToBackStack(null);

// Commit the transaction
fragmentTransaction.commit();
}
}
}
}

最佳答案

FragmentManager 在附加到 Activity 之前将为 null。因此请使用以下代码,如果它是一个嵌套 fragment ,请为您的 fragment 类使用 this.getChildFragmentManager() 否则使用 getActivity().getFragmentManager()getActivity().getSupportFragmentManager()

关于android - 空指针异常 : FragmentManager. beginTransaction(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27742471/

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