DialogFragment 上的 ViewPager - java.lang.IllegalStateException:否 activity
ViewPager on DialogFragment - java.lang.IllegalStateException: No activity
我正在使用 DialogFragment 实现照片缩放视图出现在缩略图网格的顶部。它包含一个允许用户切换照片的 ViewPager。
这是 DialogFragment 的详细信息:
public class DialogContainer extends PhotoViewerDialogFragment{
public DialogContainer () {
super();
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
// request a window without the title
dialog.setContentView(R.layout.fragment_photo_viewer);
dialog.getWindow().setLayout(AppDevice.getDeviceWidth(), AppDevice.getDeviceHeight());
return dialog;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
viewPager = (CustomViewPager) view.findViewById(R.id.photo_viewer_view_pager);
photoAdapter = new PhotoViewerAdapter(getChildFragmentManager(), photoSupporter, null, null);
viewPager.setAdapter(photoAdapter);
super.onViewCreated(view, savedInstanceState);
}
}
这是对话框布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/photo_viewer_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/dark_background" >
</android.support.v4.view.ViewPager>
下面是我实现适配器的方式:
public static class PhotoViewerAdapter extends FragmentStatePagerAdapter {
private PhotoSupporter photoSupporter;
public PhotoViewerAdapter(FragmentManager fm, PhotoSupporter photoSupporter) {
super(fm);
this.photoSupporter = photoSupporter;
}
@Override
public Fragment getItem(int position) {
String src = null;
// get photo src
return PhotoViewFragment.newInstance(src);
}
@Override
public int getCount() {
Log.w(TAG, "size in adapter: " + photoSupporter.getSize());
return photoSupporter.getSize();
}
}
这是每张照片的片段:
public static class PhotoViewFragment extends Fragment {
private String imagePath;
public PhotoViewFragment() {
}
public static PhotoViewFragment newInstance(String imagePath) {
PhotoViewFragment previewFragment = new PhotoViewFragment();
// put data
Bundle bundle = new Bundle();
bundle.putString("IMAGE_PATH",
imagePath);
previewFragment.setArguments(bundle);
return previewFragment;
}
/**
* When creating, retrieve this instance's number from its arguments.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
imagePath = getArguments() != null ? getArguments().getString(
"IMAGE_PATH") : "";
Log.d("onCreate", imagePath);
}
/**
* The Fragment's UI is just a simple text view showing its instance
* number.
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ImageView imageView = new ImageView(getActivity());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
imageView.setLayoutParams(lp);
// set image by Picasso
Picasso picassoInstance = Picasso.with(getActivity());
picassoInstance.setIndicatorsEnabled(true);
picassoInstance.load(new File(imagePath))
.error(R.drawable.ic_picture_placeholder_light2)
.fit()
.centerInside()
.into(imageView);
return imageView;
}
}
}
单击缩略图时:
dialogContainer = new DialogContainer();
dialogContainer.show(((HomeActivity)parentActivity).getSupportFragmentManager(), "photo_viewer_dialog");
我得到了这个错误:
01-16 15:46:28.916: E/AndroidRuntime(18338): FATAL EXCEPTION: main
01-16 15:46:28.916: E/AndroidRuntime(18338): java.lang.IllegalStateException: No activity
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075)
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070)
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1861)
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1474)
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:931)
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.support.v4.app.FragmentManagerImpl.run(FragmentManager.java:429)
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.os.Handler.handleCallback(Handler.java:615)
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.os.Handler.dispatchMessage(Handler.java:92)
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.os.Looper.loop(Looper.java:137)
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.app.ActivityThread.main(ActivityThread.java:4745)
01-16 15:46:28.916: E/AndroidRuntime(18338): at java.lang.reflect.Method.invokeNative(Native Method)
01-16 15:46:28.916: E/AndroidRuntime(18338): at java.lang.reflect.Method.invoke(Method.java:511)
01-16 15:46:28.916: E/AndroidRuntime(18338): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-16 15:46:28.916: E/AndroidRuntime(18338): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-16 15:46:28.916: E/AndroidRuntime(18338): at dalvik.system.NativeStart.main(Native Method)
非常感谢你们的任何建议,请让我知道任何尚不清楚的地方。
ps:我确实尝试了 this, this 和 getChildFragmentManager
但没有任何改变。
显然,此异常发生是因为它找不到 HomeActivity,因为您已将 parentActivity 投射到 HomeActivity 此处,
((HomeActivity)parentActivity).getSupportFragmentManager(),
如果您在片段中调用 DialogContainer,请使用此选项
((HomeActivity)getActivity()).getSupportFragmentManager(),
如果您在 Activity
中调用 DialogContainer,也可以使用它
this.getSupportFragmentManager()
好吧,我终于找到了。原因是我在 DialogFragment 附加到 activity:
之前调用了 getChildFragmentManager
dialogContainer = new DialogContainer();
dialogContainer.show(fm, "tag");
photoAdapter = new PhotoViewerAdapter(dialogContainer .getChildFragmentManager(), photoSupporter, null, null);
请注意:每次要显示 DialogFragment 时都应重新创建其布局。
我正在使用 DialogFragment 实现照片缩放视图出现在缩略图网格的顶部。它包含一个允许用户切换照片的 ViewPager。 这是 DialogFragment 的详细信息:
public class DialogContainer extends PhotoViewerDialogFragment{
public DialogContainer () {
super();
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
// request a window without the title
dialog.setContentView(R.layout.fragment_photo_viewer);
dialog.getWindow().setLayout(AppDevice.getDeviceWidth(), AppDevice.getDeviceHeight());
return dialog;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
viewPager = (CustomViewPager) view.findViewById(R.id.photo_viewer_view_pager);
photoAdapter = new PhotoViewerAdapter(getChildFragmentManager(), photoSupporter, null, null);
viewPager.setAdapter(photoAdapter);
super.onViewCreated(view, savedInstanceState);
}
}
这是对话框布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/photo_viewer_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/dark_background" >
</android.support.v4.view.ViewPager>
下面是我实现适配器的方式:
public static class PhotoViewerAdapter extends FragmentStatePagerAdapter {
private PhotoSupporter photoSupporter;
public PhotoViewerAdapter(FragmentManager fm, PhotoSupporter photoSupporter) {
super(fm);
this.photoSupporter = photoSupporter;
}
@Override
public Fragment getItem(int position) {
String src = null;
// get photo src
return PhotoViewFragment.newInstance(src);
}
@Override
public int getCount() {
Log.w(TAG, "size in adapter: " + photoSupporter.getSize());
return photoSupporter.getSize();
}
}
这是每张照片的片段:
public static class PhotoViewFragment extends Fragment {
private String imagePath;
public PhotoViewFragment() {
}
public static PhotoViewFragment newInstance(String imagePath) {
PhotoViewFragment previewFragment = new PhotoViewFragment();
// put data
Bundle bundle = new Bundle();
bundle.putString("IMAGE_PATH",
imagePath);
previewFragment.setArguments(bundle);
return previewFragment;
}
/**
* When creating, retrieve this instance's number from its arguments.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
imagePath = getArguments() != null ? getArguments().getString(
"IMAGE_PATH") : "";
Log.d("onCreate", imagePath);
}
/**
* The Fragment's UI is just a simple text view showing its instance
* number.
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ImageView imageView = new ImageView(getActivity());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
imageView.setLayoutParams(lp);
// set image by Picasso
Picasso picassoInstance = Picasso.with(getActivity());
picassoInstance.setIndicatorsEnabled(true);
picassoInstance.load(new File(imagePath))
.error(R.drawable.ic_picture_placeholder_light2)
.fit()
.centerInside()
.into(imageView);
return imageView;
}
}
}
单击缩略图时:
dialogContainer = new DialogContainer();
dialogContainer.show(((HomeActivity)parentActivity).getSupportFragmentManager(), "photo_viewer_dialog");
我得到了这个错误:
01-16 15:46:28.916: E/AndroidRuntime(18338): FATAL EXCEPTION: main
01-16 15:46:28.916: E/AndroidRuntime(18338): java.lang.IllegalStateException: No activity
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075)
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070)
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1861)
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1474)
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:931)
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.support.v4.app.FragmentManagerImpl.run(FragmentManager.java:429)
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.os.Handler.handleCallback(Handler.java:615)
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.os.Handler.dispatchMessage(Handler.java:92)
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.os.Looper.loop(Looper.java:137)
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.app.ActivityThread.main(ActivityThread.java:4745)
01-16 15:46:28.916: E/AndroidRuntime(18338): at java.lang.reflect.Method.invokeNative(Native Method)
01-16 15:46:28.916: E/AndroidRuntime(18338): at java.lang.reflect.Method.invoke(Method.java:511)
01-16 15:46:28.916: E/AndroidRuntime(18338): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-16 15:46:28.916: E/AndroidRuntime(18338): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-16 15:46:28.916: E/AndroidRuntime(18338): at dalvik.system.NativeStart.main(Native Method)
非常感谢你们的任何建议,请让我知道任何尚不清楚的地方。
ps:我确实尝试了 this, this 和 getChildFragmentManager
但没有任何改变。
显然,此异常发生是因为它找不到 HomeActivity,因为您已将 parentActivity 投射到 HomeActivity 此处,
((HomeActivity)parentActivity).getSupportFragmentManager(),
如果您在片段中调用 DialogContainer,请使用此选项
((HomeActivity)getActivity()).getSupportFragmentManager(),
如果您在 Activity
中调用 DialogContainer,也可以使用它this.getSupportFragmentManager()
好吧,我终于找到了。原因是我在 DialogFragment 附加到 activity:
之前调用了getChildFragmentManager
dialogContainer = new DialogContainer();
dialogContainer.show(fm, "tag");
photoAdapter = new PhotoViewerAdapter(dialogContainer .getChildFragmentManager(), photoSupporter, null, null);
请注意:每次要显示 DialogFragment 时都应重新创建其布局。