2

I've seen many questions that seemed relevant, but I have failed to found the one that is exactly what I'm looking for.

I have an App, with a ViewPager in MainActivity. one of the Fragments in the ViewPager is a ListFragment. I want to enable a creation of new Fragment when the user clicks a List item, using onListItemClick function. The new Fragment will replace the ViewPager, and will display its layout and data. I also want to allows clicking the Back button, which will return the User to the previous state of the PageViewer.

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
        StationDescriptionFragment newFragment = new StationDescriptionFragment();
        Bundle args = new Bundle();
        args.putString(StationDescriptionFragment.ARG_DESCRIPTION, this.stationsList.get(position).getDescription());
        newFragment.setArguments(args);
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment_container, newFragment);
        transaction.addToBackStack(null);
        transaction.commit();
        getListView().setItemChecked(position, true);
    }

This code is inside the Fragment which is part of the ViewPager. This code ends and than the app crashes right after this function ends. I know for sure that this function ends.

The crash is:

FragmentManagerImpl.throwException(RuntimeException) line: 462

The StationDescriptionFragment class:

public class StationDescriptionFragment extends Fragment {
    final static String ARG_DESCRIPTION = "description";
    String mCurrentDescription = "";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            mCurrentDescription = savedInstanceState.getString(ARG_DESCRIPTION);
        }
        return inflater.inflate(R.layout.description_view, container, false);
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        updateDescription("Some Description");
    }

    public void updateDescription(String description) {
        TextView desc = (TextView) getActivity().findViewById(R.id.description);
        desc.setText(description);
        mCurrentDescription = description;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(ARG_DESCRIPTION, mCurrentDescription);
    }
}

description_view.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/description"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:textSize="18sp" />

Any help will be much appreciated. Thanks.

4

1 回答 1

1

新的 Fragment 将替换 ViewPager,并将显示其布局和数据。我还希望允许单击“后退”按钮,这将使用户返回到 PageViewer 的先前状态。

如果您想替换ViewPager(我假设在另一个片段中)及其页面片段,则使用普通FragmentManager而不是返回的getChildFragmentManager()那个旨在实现嵌套片段。getChildFragmentManager()将在当前片段的视图中查找该 id(以放置新片段),我假设R.id.fragment_container是保存片段的容器的 id,其中ViewPager

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
        getListView().setItemChecked(position, true); // call this before the transaction 
        StationDescriptionFragment newFragment = new StationDescriptionFragment();
        Bundle args = new Bundle();
        args.putString(StationDescriptionFragment.ARG_DESCRIPTION, this.stationsList.get(position).getDescription());
        newFragment.setArguments(args);
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment_container, newFragment);
        transaction.addToBackStack(null);
        transaction.commit();
}
于 2013-08-09T06:10:41.430 回答