aaearon
aaearon

Reputation: 180

Crash when returning to fragment as a tab. View with same id

My main activity has an ActionBar with two tabs. Tab 1 is a ListFragment and Tab 2 is a fragment that uses the HeaderListView library. When you open the app, tab 1 shows. When you switch to tab 2, then back to tab 1, and finally return to tab 2 the app crashes with the following:

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 is only used in tab 2 and only in the onCreateView() method and isn't inflated in tab 1. I'm perplexed as to where to go from here. Also for what its worth I am using android.app.v4.support.Fragment

Tab 2's fragment's relevant parts:

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;
    }
}

The relevant parts of MainActivity that instantiate the fragments and the tabs:

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();

    }
}

And just in case two methods from the TabListener class I have:

 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);
    }

Upvotes: 4

Views: 860

Answers (1)

aaearon
aaearon

Reputation: 180

This is an issue in HeaderListView. Setting an id on the listview (listView.setId(1)) to any integer solves the problem.

Upvotes: 2

Related Questions