Android兼容包Fragment作为内部静态类

发布于 2024-12-03 07:35:52 字数 390 浏览 1 评论 0原文

我一直在使用 Android 兼容性包,但遇到了以下问题,似乎每当我在应用程序上创建一个片段作为内部静态类并尝试启动该活动时,它都会显示以下错误

android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment org.wr.CreditCardHolderActivity.CreditCardHolderFragment: make sure class name exists, is public, and has an empty constructor that is public

并且当我分离片段时而且活动一切顺利,有人知道为什么吗?我该如何解决它?

谢谢!

I've been using the Android compatibility Package but i encountered the following issue, it seems that whenever i create a Fragment as an inner static class on my application and try to start that activity the it display the following error

android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment org.wr.CreditCardHolderActivity.CreditCardHolderFragment: make sure class name exists, is public, and has an empty constructor that is public

And when i separate the fragment and the activity everything work smoothly, anyone know why? and how can i fix it?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

熊抱啵儿 2024-12-10 07:35:52

如果您有一个内部类 Fragment,例如:

public class SomethingFragment extends Fragment {    
  public static final class TypeFragment extends BaseFragment
  {

    public static Fragment newInstance()
    {
        return new TypeFragment();
    }

    private View mRootView;
    private ListView mListView;

    /**
     * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater,
     *      android.view.ViewGroup, android.os.Bundle)
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
        mRootView = inflater.inflate(R.layout.fragment_category_list, container, false);
        mListView = (ListView) mRootView.findViewById(R.id.fragment_listview);
        return mRootView;
    }
  }
}

确保您的限定符是 public 当 FragmentActivity 尝试重新启动片段时,它不会从具体类中调用它,它将从抽象 FragmentActivity 中处理它,并且如果您的内部类 Fragment 是私有的,则 Activity 不会引用 onSaveState、onRestoreState、initialise 等。

privatepublic 为我修复了它!

更新:

查看https://android.googlesource.com/platform/frameworks/support/+/refs/heads/master/v4/java/android/support/v4/app/Fragment.java

当尝试从保存状态(片段被完全破坏)恢复片段时,instantiate 方法会调用 newInstance()

newInstance 方法要求该类可公开访问,因此当定义为内部类时,这意味着它必须在适当的情况下为 publicstatic

希望这能解决一些未来的问题。

If you have an inner class Fragment like:

public class SomethingFragment extends Fragment {    
  public static final class TypeFragment extends BaseFragment
  {

    public static Fragment newInstance()
    {
        return new TypeFragment();
    }

    private View mRootView;
    private ListView mListView;

    /**
     * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater,
     *      android.view.ViewGroup, android.os.Bundle)
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
        mRootView = inflater.inflate(R.layout.fragment_category_list, container, false);
        mListView = (ListView) mRootView.findViewById(R.id.fragment_listview);
        return mRootView;
    }
  }
}

Make sure your qualifier is public when the FragmentActivity tries to reinitiate the fragment it doesn't call it from the concrete class it will handle it from the abstract FragmentActivity, and if your inner class Fragment is private the Activity has no reference to onSaveState, onRestoreState, initialise etc..

private to public fixed it for me!

Update:

Have a look at https://android.googlesource.com/platform/frameworks/support/+/refs/heads/master/v4/java/android/support/v4/app/Fragment.java

The instantiate method calls newInstance() when trying to restore the Fragment from a saved state (where the fragment was completely destroyed).

The newInstance method requires the class to be publicly accessible, so when defined as an inner class this means it has to be public and static where appropriate.

Hope this clears up some future questions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文