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是一个独立的班级顺便说一句。
答案 0 :(得分:4)
看起来您没有为PhotoThumbnailFragmentAdapter
定义空构造函数。
片段应该有一个像
public PhotoThumbnailFragmentAdapter(){
//empty constructor
}
答案 1 :(得分:4)
让您的PhotoPreviewFragment
课程为公开和静态课程。
现在,你的PhotoPreviewFragment
是一个非静态的(即它有一个封闭的类,在你的情况下是PhotoPreviewFragmentAdapter
类)内部类,它不能公开实例化。
请确保公共空构造函数为public PhotoPreviewFragment() {... ...}
。
解决方案:
将您的PhotoPreviewFragment
重构为适当的公共静态内部类或仅在其自己的Java文件中重写公共类
请务必添加一个公共空构造函数。
答案 2 :(得分:3)
无法发表评论因此发布为答案,请仔细阅读此链接可能会有所帮助Fragment - InstantiationException: no empty Constructor -> Google Maps v2?
和这个