问题描述:定义一个MyFragment,明明有一个空的构造函数,却始终报java.lang.InstantiationException:no empty constructor错误
public class MyFragment extends Fragment {
public MyFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.new_reminder_fragment, container, false);
...
return rootView;
}
}
解决方法:
public static class MyFragment extends Fragment {
public MyFragment() {
}
将类改成静态的即可。
本文介绍了一种解决MyFragment在Android应用中出现java.lang.InstantiationException:no empty constructor异常的方法。通过将MyFragment类声明为静态类,可以有效避免此问题。
314

被折叠的 条评论
为什么被折叠?



