无法实例化片段 - java.lang.RuntimeException

时间:2013-09-30 04:52:21

标签: android fragment

Hy先生,我从我的开发者控制台获得了这个堆栈跟踪。好吧,我不确定导致这次崩溃的原因。因为我在测试我的应用程序时从来没有得到这个错误,只是从谷歌播放的开发者控制台获得它。

java.lang.RuntimeException: Unable to start activity ComponentInfo{package.name/package.name.activity.SomeActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment package.name.photopreview.PhotoThumbnailFragmentAdapter$1: make sure class name exists, is public, and has an empty constructor that is public
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
at android.app.ActivityThread.access$700(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment streetdirectory.mobile.modules.photopreview.PhotoThumbnailFragmentAdapter$1: make sure class name exists, is public, and has an empty constructor that is public
at android.support.v4.app.Fragment.instantiate(Fragment.java:399)
at android.support.v4.app.FragmentState.instantiate(Fragment.java:97)
at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1760)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:200)
at streetdirectory.mobile.modules.businessdetail.BusinessDetailActivity.onCreate(BusinessDetailActivity.java:134)
at android.app.Activity.performCreate(Activity.java:5188)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
... 11 more
Caused by: java.lang.InstantiationException: can't instantiate class streetdirectory.mobile.modules.photopreview.PhotoThumbnailFragmentAdapter$1; no empty constructor
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.support.v4.app.Fragment.instantiate(Fragment.java:388)
... 18 more

任何人都可以帮我分析这个问题吗?并给我一些有用的建议。 谢谢,问候......

修改

这是PhotoPreviewFragmentAdapter代码。

public class PhotoPreviewFragmentAdapter<T extends ImageListServiceOutput> extends FragmentPagerAdapter {

    private ArrayList<T> mData = new ArrayList<T>();

    public PhotoPreviewFragmentAdapter(FragmentManager fm) {
        super(fm);
    }

    public void setData(ArrayList<T> data) {
        mData = data;
    }


    @SuppressLint("ValidFragment")
    @Override
    public Fragment getItem(final int position) {
        PhotoPreviewFragment view = new PhotoPreviewFragment() {

            @Override
            public ImageListServiceOutput getData() {
                if (position < mData.size()) {
                    return mData.get(position);
                }
                return null;
            }
        };

        return view;
    }

    @Override
    public int getCount() {
        // TODO Add Photo Button
        //return mData.size()+1;
        return mData.size();
    }


}
给出一些关于如何重现这次崩溃的线索可能会对我有所帮助。感谢

修改

可能问题是(就像 Streets Of Boston 所说的)PhotoTumbnailFragmentAdapter中的匿名内部类,我在这个方法中使用它

public Fragment getItem(final int position) {
        PhotoThumbnailFragment view = new PhotoThumbnailFragment() {

            @Override
            public ImageListServiceOutput getData() {
                if (position < mData.size()) {
                    return mData.get(position);
                }
                return null;
            }

            public void onPhotoClicked(ImageListServiceOutput data) {

                if (mImageClickedListener != null) {
                    if (data != null) {
                        Logger.info("Photo Clicked");
                        mImageClickedListener.onImageClicked(data, position);
                    } else {
                        Logger.info("Add Photo Clicked");
                        mImageClickedListener.onAddImageClicked(position);
                    }
                }
            }
        };

        return view;
    }

PhotoThumbnailFragment是一个独立的班级顺便说一句。

3 个答案:

答案 0 :(得分:4)

看起来您没有为PhotoThumbnailFragmentAdapter定义空构造函数。 片段应该有一个像

这样的空构造函数
public PhotoThumbnailFragmentAdapter(){
//empty constructor
}

答案 1 :(得分:4)

  1. 让您的PhotoPreviewFragment课程为公开静态课程。 现在,你的PhotoPreviewFragment是一个非静态的(即它有一个封闭的类,在你的情况下是PhotoPreviewFragmentAdapter类)内部类,它不能公开实例化。

  2. 请确保公共空构造函数为public PhotoPreviewFragment() {... ...}

  3. 解决方案:
    将您的PhotoPreviewFragment重构为适当的公共静态内部类或仅在其自己的Java文件中重写公共类 请务必添加一个公共空构造函数。

答案 2 :(得分:3)

无法发表评论因此发布为答案,请仔细阅读此链接可能会有所帮助Fragment - InstantiationException: no empty Constructor -> Google Maps v2?

和这个

Do fragments really need an empty constructor?