3

Hy guys, i got this stack trace from my developer console. Okay i'm not sure what cause this crash. because i never get this error by my self when test my app and just got it from developer console in google play.

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

anybody can help me analyze this problem ? and give some useful suggestion to me . thanks, regards ..

EDIT

This is PhotoPreviewFragmentAdapter code.

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();
    }


}

give some clue about how to reproduce this crash may be will help me so much. thanks

EDIT

May be the problem is (just like Streets Of Boston said) the anonymous inner class in PhotoTumbnailFragmentAdapter, i used it in this method

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 is an independent class btw.

Khairil Ushan
  • 2,358
  • 5
  • 26
  • 29
  • Can you post your `PhotoThumbnailFragmentAdapter` code ? – GrIsHu Sep 30 '13 at 05:32
  • You used annonymous PhotoPreviewFragment. You should not use annonymous fragment class. Change it into non-annonymous class. If you should access `mData`, you can access it by implementing setter method for `mData` – kingori Sep 30 '13 at 07:00

3 Answers3

4

It looks like you have not defined empty constructor for your PhotoThumbnailFragmentAdapter. Fragment should have an empty constructor like

public PhotoThumbnailFragmentAdapter(){
//empty constructor
}
Rajiv Ratan
  • 129
  • 4
4
  1. Make your PhotoPreviewFragment class a public and static class. Right now, your PhotoPreviewFragment is a non-static (i.e. it has an enclosing class, in your case the PhotoPreviewFragmentAdapter class) inner class, which cannot be publicly instantiated.

  2. Be sure to have a public empty constructor public PhotoPreviewFragment() {... ...}.

Solution:
Refactor your PhotoPreviewFragment to a proper public static inner class or just a public class in its own Java file.
Be sure to add a public empty constructor.

Streets Of Boston
  • 12,576
  • 2
  • 25
  • 28
  • (1). my Fragment is not an inner class. it an independent java class (one .java file) (2). i have added an empty constructor, but still get the same error. – Khairil Ushan Oct 01 '13 at 10:12
  • 1
    In your code example in your original question, your fragment is an inner class. Its non-public(/anonymous) class name is "PhotoThumbnailFragmentAdapter$1" The '$1' at the end hints at it being an non-public class and it is clear from your code it's an inner non-public class. – Streets Of Boston Oct 01 '13 at 12:02
  • mmmm i've edited my question, can u look at that ? may be you can give me some advice how to solve this. so i have 2 java files here, PhotoThumbnailFragment.java and PhotoThumbnailFragmentAdapter.java – Khairil Ushan Oct 02 '13 at 02:56
  • 1
    But you subclassed PhotoThumbnailFragment into a new inner anonymous sub-class: "view = new PhotoThumbnailFragment() { ... ... };" You need to create a new static public sub-class of PhotoThumbnailFragment instead, that overrides these two methods (getData and onPhotoClicked). – Streets Of Boston Oct 02 '13 at 14:01
3

can't comment hence posting as an answer, please go through this link may be this will help Fragment - InstantiationException: no empty Constructor -> Google Maps v2?

and this

Do fragments really need an empty constructor?

Community
  • 1
  • 1
Nitin Joshi
  • 128
  • 1
  • 10