3

嗨,伙计们,我从我的开发人员控制台获得了这个堆栈跟踪。好的,我不确定是什么导致了这次崩溃。因为我在测试我的应用程序时从来没有自己遇到过这个错误,只是从谷歌游戏的开发者控制台中得到它。

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 是一个独立的类。

4

3 回答 3

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

  2. 确保有一个公共的空构造函数 public PhotoPreviewFragment() {... ...}

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

于 2013-09-30T16:54:13.003 回答
4

看起来你还没有为你的PhotoThumbnailFragmentAdapter. 片段应该有一个空的构造函数,如

public PhotoThumbnailFragmentAdapter(){
//empty constructor
}
于 2013-09-30T05:10:11.543 回答
3

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

和这个

片段真的需要一个空的构造函数吗?

于 2013-09-30T06:41:25.723 回答