EDIT3:似乎只有 API17 及更高版本才能让 ViewPager 与 ChildFragmentManager 一起正常工作............
我正在尝试向ViewPager
我的 DialogFragment 添加一个:
public class FirstLaunchDialogFragment extends DialogFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, R.style.Theme_Sherlock_Light_Dialog);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final View v = getActivity().getLayoutInflater().inflate(
R.layout.fragment_dialog_first_launch, null);
ViewPager viewPager = (ViewPager) v.findViewById(R.id.pager);
viewPager.setAdapter(new FirstLaunchFragmentsAdapter(getChildFragmentManager()));
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(v).setInverseBackgroundForced(true);
return builder.create();
}
}
由于某些原因,它会崩溃说:
04-06 00:00:49.600: E/AndroidRuntime(3734): java.lang.IllegalStateException: Fragment does not have a view
04-06 00:00:49.600: E/AndroidRuntime(3734): at android.support.v4.app.Fragment$1.findViewById(Fragment.java:1425)
04-06 00:00:49.600: E/AndroidRuntime(3734): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:901)
04-06 00:00:49.600: E/AndroidRuntime(3734): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
04-06 00:00:49.600: E/AndroidRuntime(3734): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-06 00:00:49.600: E/AndroidRuntime(3734): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
04-06 00:00:49.600: E/AndroidRuntime(3734): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
.....
我已经在谷歌上搜索Fragment does not have a view
了半天没有结果
在我看来,适配器无法膨胀布局或其他东西......
这是我的片段和适配器
public static class UserWelcomeFragment extends Fragment {
public static UserWelcomeFragment newInstance() {
return new UserWelcomeFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_welcome_user, container, false);
return v;
}
}
private class FirstLaunchFragmentsAdapter extends FragmentPagerAdapter {
public FirstLaunchFragmentsAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int arg0) {
switch (arg0) {
case 0:
return UserWelcomeFragment.newInstance();
case 1:
return new IntegrationSelectingDialogFragment();
default:
return UserWelcomeFragment.newInstance();
}
}
@Override
public int getCount() {
return 2;
}
}
编辑:https ://code.google.com/p/android/issues/detail?id=42601看起来相关
EDIT2:我正在对 API 16 进行测试,并且该项目正在使用支持库 v4 修订版 12