我正在尝试使用PageViewer
inside DialogFragment
。
我的问题是显示对话框时出现此错误:
10-18 00:39:30.985: E/AndroidRuntime(21376): FATAL EXCEPTION: main
10-18 00:39:30.985: E/AndroidRuntime(21376): java.lang.IllegalStateException: Fragment does not have a view
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.support.v4.app.Fragment$1.findViewById(Fragment.java:1443)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:918)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1105)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1461)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.view.View.measure(View.java:15525)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
10-18 00:39:30.985: E/AndroidRuntime(21376): at org.holoeverywhere.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:594)
10-18 00:39:30.985: E/AndroidRuntime(21376): at org.holoeverywhere.widget.LinearLayout.measureVertical(LinearLayout.java:913)
10-18 00:39:30.985: E/AndroidRuntime(21376): at org.holoeverywhere.widget.LinearLayout.onMeasure(LinearLayout.java:1105)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.view.View.measure(View.java:15525)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.view.View.measure(View.java:15525)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.view.View.measure(View.java:15525)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
10-18 00:39:30.985: E/AndroidRuntime(21376): at org.holoeverywhere.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:594)
10-18 00:39:30.985: E/AndroidRuntime(21376): at org.holoeverywhere.widget.LinearLayout.measureVertical(LinearLayout.java:913)
10-18 00:39:30.985: E/AndroidRuntime(21376): at org.holoeverywhere.widget.LinearLayout.onMeasure(LinearLayout.java:1105)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.view.View.measure(View.java:15525)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-18 00:39:30.985: E/AndroidRuntime(21376): at org.holoeverywhere.internal.WindowDecorView.onMeasure(WindowDecorView.java:114)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.view.View.measure(View.java:15525)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.view.View.measure(View.java:15525)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.view.View.measure(View.java:15525)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-18 00:39:30.985: E/AndroidRuntime(21376): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2434)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.view.View.measure(View.java:15525)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.view.Choreographer.doFrame(Choreographer.java:532)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.os.Handler.handleCallback(Handler.java:725)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.os.Handler.dispatchMessage(Handler.java:92)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.os.Looper.loop(Looper.java:137)
10-18 00:39:30.985: E/AndroidRuntime(21376): at android.app.ActivityThread.main(ActivityThread.java:5227)
10-18 00:39:30.985: E/AndroidRuntime(21376): at java.lang.reflect.Method.invokeNative(Native Method)
10-18 00:39:30.985: E/AndroidRuntime(21376): at java.lang.reflect.Method.invoke(Method.java:511)
10-18 00:39:30.985: E/AndroidRuntime(21376): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
10-18 00:39:30.985: E/AndroidRuntime(21376): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562
我搜索了这个问题并找到了这篇文章:
https://code.google.com/p/android/issues/detail?id=42601
帖子中的答案是使用以下代码:
getSupportFragmentManager().beginTransaction().detach(mFragment1).replace(R.id.main, mFragment2).attach(mFragment2).addToBackStack(null).commit();
在帖子中没有太多关于如何使用它的信息。
- 什么是 mFragment1?
- 什么是 mFragment2?
- 我如何访问 mFragment1?
- 我如何访问 mFragment2?
- R.id.main 是什么意思?
- 在我的代码中什么时候应该使用它?
要调用我使用的对话框:
private static ScreenSlidePagerAdapter mPagerAdapter;
private static ViewPager mPager;
new PostDialogFragment().show(getSupportFragmentManager(), "Dialog");
这是我的代码,感谢您的帮助:)
class PostDialogFragment extends DialogFragment {
public PostDialogFragment() {
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(WorkoutsWall.this);
saveLoadedWorkouts = workoutsList;
builder.setTitle(workoutsList[poisition][0]);
View prefView = getActivity().getLayoutInflater().inflate(R.layout.wall_posts, null);
builder.setView(prefView);
isWorkoutDetailsShown = true;
pictures = new String[2];
newPostsLoaded = false;
String positvieText = null;
String netualText = null;
mPager = (ViewPager) prefView.findViewById(R.id.pager);
mPager.setPageTransformer(true, new com.example.workoutlog.ZoomOutPageTransformer());
mPager.setOnPageChangeListener(WorkoutsWall.this);
commentsButtons = (Button) prefView.findViewById(R.id.extraTab);
postButtons = (Button) prefView.findViewById(R.id.homePageTab);
commentsButtons.setOnClickListener(WorkoutsWall.this);
postButtons.setOnClickListener(WorkoutsWall.this);
commentsLinearDialog = (LinearLayout) prefView.findViewById(R.id.extraLine);
postLinearDialog = (LinearLayout) prefView.findViewById(R.id.homeLine);
if(dialogFromPost)
{
commentsLinearDialog.setBackgroundColor(Color.BLACK);
postLinearDialog.setBackgroundColor(Color.BLUE);
}
else
{
postLinearDialog.setBackgroundColor(Color.BLACK);
commentsLinearDialog.setBackgroundColor(Color.BLUE);
}
if(dialogFromPost)
{
positvieText = "Add to your routines";
netualText = "Add user to favorites";
}
else
{
positvieText = "add comment";
netualText = "refresh";
}
builder.setPositiveButton(positvieText,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
builder.setNeutralButton(netualText,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
mPagerAdapter = new ScreenSlidePagerAdapter(getChildFragmentManager());
mPager.setAdapter(mPagerAdapter);
return builder.create();
}
}
public static class CommentsFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.show_comments, container, false);
editComment = (EditText) rootView.findViewById(R.id.editComment);
messageLength = (TextView) rootView.findViewById(R.id.messageLength);
commentsLayout = (WallScrollView) rootView.findViewById(R.id.commentsLayout);
commnentUp = (ImageButton) rootView.findViewById(R.id.fastUp);
commentsDown = (ImageButton) rootView.findViewById(R.id.fastDown);
return rootView;
}
}
public static class PostFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.workout_wall_show, container, false);
ExpandList = (ExpandableListView) rootView.findViewById(R.id.wallDetailsShowList);
playYouTube = (ImageButton) rootView.findViewById(R.id.playYouTube);
galery = (Gallery) rootView.findViewById(R.id.galleryImages);
return rootView;
}
}
class ScreenSlidePagerAdapter extends FragmentPagerAdapter {
@Override
public int getItemPosition(Object object){
return PagerAdapter.POSITION_NONE;
}
public ScreenSlidePagerAdapter(FragmentManager fm)
{
super(fm);
}
@Override
public Fragment getItem(int position) {
if(position == 1)
return new PostFragment();
else
return new CommentsFragment();
}
@Override
public int getCount() {
return 2;
}
}