android – 返回片段作为选项卡时崩溃.查看具有相同的ID

我的主要活动有一个带有两个标签的ActionBar.选项卡1是ListFragment,选项卡2是使用HeaderListView库的片段.当您打开应用程序时,选项卡1显示.当您切换到选项卡2,然后返回选项卡1,最后返回选项卡2时,应用程序崩溃时出现以下情况:

09-12 13:19:22.440  21772-21772/com.iosharp.android.smoothstreams E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.iosharp.android.smoothstreams, PID: 21772
    java.lang.IllegalArgumentException: Wrong state class, expecting View State but received class android.widget.AbsListView$SavedState instead. This usually happens when two views of different type have the same id in the same hierarchy. This view's id is id/header_list_view. Make sure other views do not use the same id.

id / header_list_view仅在选项卡2中使用,仅在onCreateView()方法中使用,并且不在选项卡1中膨胀.我对从何处开始感到困惑.同样值得我使用android.app.v4.support.Fragment

标签2的片段的相关部分:

public class ProgrammeListFragment extends Fragment {
    private static final String TAG = ProgrammeListFragment.class.getSimpleName();
    private ArrayList<Programme> mProgrammes;
    private ArrayList<Day> mDays;
    private Date mNow;
    private HeaderListView mHeaderListView;
    static private SectionAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mNow = new Date();

        mProgrammes = ProgrammeStore.get(getActivity()).getProgrammes();
        Collections.sort(mProgrammes, new Programme());
        mProgrammes = trimProgrammes(mProgrammes);

        mDays = setupDays(mProgrammes);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.progamme_header_list, container, false);
        mHeaderListView = (HeaderListView) v.findViewById(R.id.header_list_view);
        mAdapter = new SectionAdapter() {
            @Override
            public int numberOfSections() {
                return mDays.size();
            }

            @Override
            // TODO: sometimes -1 is passed as int section, this needs to be looked at later
            public int numberOfRows(int section) {
                if (section > -1) {
                    return mDays.get(section).getLineUp().size();
                } else {
                    return 1;
                }
            }

            @Override
            public View getRowView(int section, int row, View convertView, ViewGroup parent) {
                convertView = getActivity().getLayoutInflater().inflate(
                        R.layout.programme_list_item, null);

                //set views

                return convertView;
            }

            @Override
            public Day getSectionHeaderItem(int section) {
                return mDays.get(section);
            }

            @Override
            public int getSectionHeaderItemViewType(int section) {
                return super.getSectionHeaderItemViewType(section);
            }

            @Override
            public boolean hasSectionHeaderView(int section) {
                return true;
            }

            @Override
            public View getSectionHeaderView(int section, View convertView, ViewGroup parent) {
                convertView = getActivity().getLayoutInflater().inflate(R.layout.programme_list_header, null);

                // set views

                return convertView;
            }

            @Override
            public Programme getRowItem(int section, int row) {
                return mDays.get(section).getLineUp().get(row);
            }
        };

        mHeaderListView.setAdapter(mAdapter);

        return v;
    }
}

MainActivity的相关部分,用于实例化片段和选项卡:

public class MainActivity extends ActionBarActivity {
    private static final String TAG = MainActivity.class.getSimpleName();

    private static final String KEY_TAB = "tab";

    private Fragment fragmentChannels = new ChannelListFragment();
    private Fragment fragmentProgrammes = new ProgrammeListFragment();

    private int mCurrentTab;
    private ActionBar mActionBar;
    private ArrayList<Channel> mChannels;
    private ArrayList<Programme> mProgrammes;
    private ProgressDialog mProgressDialog;

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

        mActionBar = getSupportActionBar();
        mActionBar.setTitle(R.string.app_name);
        mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        mActionBar.addTab(mActionBar.newTab().setText(R.string.channels_title).setTabListener(new TabListener(fragmentChannels)));
        mActionBar.addTab(mActionBar.newTab().setText(R.string.programmes).setTabListener(new TabListener(fragmentProgrammes)));


        // Get singletons
        mProgrammes = ProgrammeStore.get(getApplication()).getProgrammes();
        mChannels = ChannelStore.get(getApplicationContext()).getChannels();

    }
}

以防我在TabListener类中有两个方法:

 public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        fragmentTransaction.replace(R.id.fragmentContainer, fragment);
    }

    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        fragmentTransaction.remove(fragment);
    }

最佳答案 这是HeaderListView中的一个问题.将listview(listView.setId(1))上的id设置为任何整数可以解决问题.

点赞