问题
I am implementing a photo-zooming view appears on top of the thumbnail grid by using DialogFragment. It contains a ViewPager allowing user switch photos. Here is detail of 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);
}
}
Here is dialog layout:
<?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>
And here is how I implement the adapter:
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();
}
}
Here is Fragment for each photo:
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;
}
}
}
When a thumbnail is clicked:
dialogContainer = new DialogContainer();
dialogContainer.show(((HomeActivity)parentActivity).getSupportFragmentManager(), "photo_viewer_dialog");
And I got this error:
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)
Thank you guys so much for any suggestion and please let me know anything not clear yet.
ps: I did try this, this and getChildFragmentManager
but nothing changed.
回答1:
Okay, finally I found the it. The reason is I call getChildFragmentManager
on DialogFragment before it attached to activity:
dialogContainer = new DialogContainer();
dialogContainer.show(fm, "tag");
photoAdapter = new PhotoViewerAdapter(dialogContainer .getChildFragmentManager(), photoSupporter, null, null);
One more note: DialogFragment should be recreate its layout every single time you want to show it.
回答2:
Apparently,this exception occouring beacause it cannot find HomeActivity as you have Casted the parentActivity to HomeActivity here,
((HomeActivity)parentActivity).getSupportFragmentManager(),
Use this, if you are calling DialogContainer in a fragment
((HomeActivity)getActivity()).getSupportFragmentManager(),
Or use this, if you are calling DialogContainer in a Activity
this.getSupportFragmentManager()
来源:https://stackoverflow.com/questions/27980543/viewpager-on-dialogfragment-java-lang-illegalstateexception-no-activity