gpt4 book ai didi

android - 以选项卡形式返回 fragment 时崩溃。使用相同的 ID 查看

转载 作者:太空宇宙 更新时间:2023-11-03 10:19:47 28 4
gpt4 key购买 nike

我的主要 Activity 有一个带有两个选项卡的 ActionBar。 Tab 1 是 ListFragment,Tab 2 是使用 HeaderListView 库的 fragment 。当您打开应用程序时,将显示选项卡 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 中没有膨胀。我对从这里去哪里感到困惑。同样为了它的值(value),我正在使用 android.app.v4.support.Fragment

Tab 2 fragment 的相关部分:

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

实例化 fragment 和选项卡的 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 设置为任何整数即可解决问题。

关于android - 以选项卡形式返回 fragment 时崩溃。使用相同的 ID 查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25807332/

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