无法解决在FragmentManager中的super()方法错误。

huangapple go评论74阅读模式
英文:

Cannot resolve method super() error in fragmentmanager Android

问题

我不是Java程序员,但我在尝试构建一个应用程序的同时也在学习Java编程。我试图添加两个片段,一个是登录片段,另一个是注册片段,但在使用超类构造函数时,我遇到了"cannot resolve"错误。有人能指出我做错了什么吗?

我知道"super"用于访问父类构造函数,而在我的情况下父类构造函数不是空的。

以下是MainActivity的代码:

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ViewPager2 viewPager = findViewById(R.id.viewPager);

        AuthenticationPagerAdapter pagerAdapter = new AuthenticationPagerAdapter(getSupportFragmentManager());
        pagerAdapter.addFragment(new fragment_login());
        pagerAdapter.addFragment(new fragment_register());
        viewPager.setAdapter(pagerAdapter);
    }

    public static class AuthenticationPagerAdapter extends FragmentStateAdapter {
        private ArrayList<Fragment> fragmentList = new ArrayList<>();

        public AuthenticationPagerAdapter(FragmentManager fragmentActivity) {
            super(fragmentActivity);
        }

        public Fragment getItem(int i) {
            return fragmentList.get(i);
        }

        public int getCount() {
            return fragmentList.size();
        }

        void addFragment(Fragment fragment) {
            fragmentList.add(fragment);
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            return null;
        }

        @Override
        public int getItemCount() {
            return 0;
        }
    }
}
英文:

I am not a Java coder but trying to build an app while trying to learn Java along the way.
I am trying to add 2 fragments one is the login fragment and one is the register fragment but while using super constructor I am facing cannot resolve error.
Could anyone suggest what I am doing wrong?

I get that super is used for accessing the parent class constructor which in my case is not empty.

Following is the MainActivity code:

public class MainActivity extends FragmentActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ViewPager2 viewPager = findViewById(R.id.viewPager);

        AuthenticationPagerAdapter pagerAdapter = new AuthenticationPagerAdapter(getSupportFragmentManager());
        pagerAdapter.addFragment(new fragment_login());
        pagerAdapter.addFragment(new fragment_register());
        viewPager.setAdapter(pagerAdapter);
    }

    public static class AuthenticationPagerAdapter extends FragmentStateAdapter
    {
        private ArrayList&lt;Fragment&gt; fragmentList = new ArrayList&lt;&gt;();

        public AuthenticationPagerAdapter(FragmentManager fragmentActivity)
        {
            super(fragmentActivity);
        }

        public Fragment getItem(int i)
        {
            return fragmentList.get(i);
        }

        public int getCount()
        {
            return fragmentList.size();
        }

        void addFragment(Fragment fragment) {
            fragmentList.add(fragment);
        }

        @NonNull
        @Override
        public Fragment createFragment(int position)
        {
            return null;
        }

        @Override
        public int getItemCount()
        {
            return 0;
        }
    }
}

答案1

得分: 0

我认为对于 fragmentActivity 参数,使用 super() 是不可能的。您应该从更高级的构造函数向 super() 方法发送一个值。顺便说一下,我看不到 MainActivity 类的构造函数。您想从哪个类中获取值?

英文:

I think it's impossible using super() for fragmentActivity parameter. You should send a values to super() method from higher situated constructor. By the way I can't see a constructor for MainActivity class. From which one class you want get a values?

答案2

得分: 0

使用super()在构造函数中,你试图调用父类的构造函数。

在这种情况下,FragmentStateAdapter 有3个构造函数:

FragmentStateAdapter(FragmentActivity fragmentActivity)
FragmentStateAdapter(Fragment fragment)
FragmentStateAdapter(FragmentManager fragmentManager,Lifecycle lifecycle)

在你的情况下:

public AuthenticationPagerAdapter(FragmentManager fragmentActivity)

super(FragmentManager); 不匹配 任何超类构造函数。

你可以使用类似这样的代码:

public AuthenticationPagerAdapter(FragmentActivity fragmentActivity) {
        super(fragmentActivity);
    }

public AuthenticationPagerAdapter(FragmentManager fragmentManager, Lifecycle lifecycle) {
        super(fragmentManager, lifecycle);
    }
英文:

Using the super() in a constructor you are trying to call the constructor of the parent class.

In this case the FragmentStateAdapter has 3 constructors:

FragmentStateAdapter(FragmentActivity fragmentActivity)
FragmentStateAdapter(Fragment fragment)
FragmentStateAdapter(FragmentManager fragmentManager, Lifecycle lifecycle)

In your case you have:

public AuthenticationPagerAdapter(FragmentManager fragmentActivity)

and super(FragmentManager); doesn't match any super constructor.

You can use something like:

public AuthenticationPagerAdapter(FragmentActivity fragmentActivity) {
        super(fragmentActivity);
    }

public AuthenticationPagerAdapter(FragmentManager fragmentManager,Lifecycle lifecycle) {
        super(fragmentManager, lifecycle);
    }

huangapple
  • 本文由 发表于 2020年8月30日 23:37:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63659135.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定