Android 加载片段时出现ViewPager错误
我有3个片段被用在一个viewpager中。我用一个FragmentPagerAdapter在它们之间切换。我一直都会犯这个错误,我不知道为什么Android 加载片段时出现ViewPager错误,android,instantiationexception,Android,Instantiationexception,我有3个片段被用在一个viewpager中。我用一个FragmentPagerAdapter在它们之间切换。我一直都会犯这个错误,我不知道为什么 android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment ... make sure class name exists, is public, and has an empty constructor that is public
android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment ... make sure class name exists, is public, and has an empty constructor that is public
这是我的getItem方法。我有一个静态方法来获取实例,但它仍然会抱怨
@Override
public Fragment getItem(int pos) {
if (SECTION_COUNT > 1){
switch(pos){
case RECENT_HISTREES:
return HistreesFragmentPhone.newInstance(HistreesViewType.Friends, accountId, userId);
case MY_HISTREES:
return HistreesFragmentPhone.newInstance(HistreesViewType.All, accountId, userId);
case DOWNLOADED_HISTREES:
return HistreesFragmentPhone.newInstance(HistreesViewType.Downloaded, accountId, userId);
}
}
return HistreesFragmentPhone.newInstance(HistreesViewType.All, accountId, userId);
}
这是我的片段构造函数
public static HistreesFragmentPhone newInstance(HistreesViewType viewType, int accountId, int userId) {
HistreesFragmentPhone f = new HistreesFragmentPhone();
Bundle args = new Bundle();
args.putString("viewType", viewType.toString());
args.putInt("accountId", accountId);
args.putInt("userId", userId);
f.setArguments(args);
return f;
}
我自己还没有完全弄清楚这一点,但我认为这通常是一个“假”异常,因为存在一些潜在的异常,这就是问题所在。InstanceionException是RuntimeException的一个子类,每当我得到RuntimeException时,总会有一些罪魁祸首在下面,这些罪魁祸首只在Logcat的完整扩展堆栈跟踪中可见。令人沮丧的是,我目前有一个实例化异常,它是从用户那里滚来的,我无法复制,而谷歌给我的堆栈跟踪在我获得足够信息之前就切断了按钮!我正在尝试的另一件事与我之前的评论相矛盾——InstanceException的错误消息说“确保类名存在,是公共的,并且有一个公共的空构造函数”。我的内部静态类没有标记为public,也没有空的公共构造函数。也许这是一些奇怪的边缘情况,只在一些随机情况下才有意义?如果这对我有帮助,或者对你有帮助,我会把它作为正式答案贴在这里的帖子上。