我正在使用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$1.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
但没有改变。
答案 0 :(得分:2)
好的,最后我发现了它。原因是我在DialogFragment上调用getChildFragmentManager
之后才附加到活动:
dialogContainer = new DialogContainer();
dialogContainer.show(fm, "tag");
photoAdapter = new PhotoViewerAdapter(dialogContainer .getChildFragmentManager(), photoSupporter, null, null);
还有一点需要注意:DialogFragment应该每次都要重新显示它的布局。
答案 1 :(得分:0)
显然,这个异常会因为找不到HomeActivity,因为你在这里将parentActivity转换为HomeActivity,
((HomeActivity)parentActivity).getSupportFragmentManager(),
如果您在片段
中调用DialogContainer,请使用此选项((HomeActivity)getActivity()).getSupportFragmentManager(),
如果您在Activity
中调用DialogContainer,请使用此选项this.getSupportFragmentManager()