Khairil Ushan
Khairil Ushan

Reputation: 2468

Unable to instantiate fragment - java.lang.RuntimeException

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.

Upvotes: 3

Views: 10275

Answers (3)

Streets Of Boston
Streets Of Boston

Reputation: 12596

  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.

Upvotes: 4

Nitin Joshi
Nitin Joshi

Reputation: 128

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?

Upvotes: 3

Rajiv Ratan
Rajiv Ratan

Reputation: 129

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

public PhotoThumbnailFragmentAdapter(){
//empty constructor
}

Upvotes: 4

Related Questions