android:如何修复类 CardsActivity.CardPagerAdapter 中的错误构造函数 CardPagerAdapter 不能应用于给定类型

Posted

技术标签:

【中文标题】android:如何修复类 CardsActivity.CardPagerAdapter 中的错误构造函数 CardPagerAdapter 不能应用于给定类型【英文标题】:android: how to fix error constructor CardPagerAdapter in class CardsActivity.CardPagerAdapter cannot be applied to given types 【发布时间】:2017-04-02 09:54:34 【问题描述】:

源码:https://github.com/jamesmccann/android-view-pager-cards/blob/master/app/src/main/java/nz/jamesmccann/cardviewpager/CardActivity.java 我下载了它,它不起作用,所以我复制到我的项目中。但是发生了一些错误,尽管我搜索了很多次,但我不知道如何解决。 这是 CarContainerFragment:

 import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class CardContainerFragment extends Fragment 

    private boolean cardFlipped = false;

    public CardContainerFragment() 
        setHasOptionsMenu(true);
    

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) 
        View rootView = inflater.inflate(R.layout.fragmentcardcontainer, container, false);

        getChildFragmentManager()
                .beginTransaction()
                .add(R.id.container, new CardFrontFragment())
                .commit();

        return rootView;
    

    public void flipCard() 
        Fragment newFragment;
        if (cardFlipped) 
            newFragment = new CardFrontFragment();
         else 
            newFragment = new CardBackFragment();
        

        getChildFragmentManager()
                .beginTransaction()
                .setCustomAnimations(
                        R.anim.card_flip_right_in, R.anim.card_flip_right_out,
                        R.anim.card_flip_left_in, R.anim.card_flip_left_out)
                .replace(R.id.container, newFragment)
                .commit();

        cardFlipped = !cardFlipped;
    

    public static class CardFrontFragment extends Fragment 

        public CardFrontFragment() 
        

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) 
            View rootView = inflater.inflate(R.layout.fragmentcard, container, false);
            return rootView;
        
    

    public static class CardBackFragment extends Fragment 

        public CardBackFragment() 
        

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) 
            View rootView = inflater.inflate(R.layout.fragment_card_back, container, false);
            return rootView;
        
    

这是 CardsActivity:

    import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.app.FragmentPagerAdapter;


public class CardsActivity extends Activity 


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

        CardPagerAdapter adapter = new CardPagerAdapter(getFragmentManager());
        ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
        viewPager.setAdapter(adapter);
    

    public class CardPagerAdapter extends FragmentPagerAdapter 

        public CardPagerAdapter(FragmentManager fragmentManager) 
            super(fragmentManager);
        

        @Override
        public Fragment getItem(int position) 
            return new Fragment();
        

        @Override
        public int getCount() 
            return 5;
        
    

这是我得到的错误:

  Error:(24, 36) error: constructor CardPagerAdapter in class CardsActivity.CardPagerAdapter cannot be applied to given types;
required: android.support.v4.app.FragmentManager
found: android.app.FragmentManager
reason: actual argument android.app.FragmentManager cannot be converted to android.support.v4.app.FragmentManager by method invocation conversion

【问题讨论】:

【参考方案1】:

你的 CardsActivity 的导入与 github 链接上的不同。

你的

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.app.FragmentPagerAdapter;

Github

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.view.ViewPager;

在我看来,您应该使用getSupportFragmentManager() 而不是getFragmentManager()(您必须从android.support.v4.app.FragmentActivity 扩展而不是从Activity 扩展才能获得此方法)。

【讨论】:

我没有看到 getSupportFragmentManager() 虽然我添加了 android.support.v4.app.FragmentActivity。 在这里developer.android.com/reference/android/support/v4/app/… 只需扩展您的活动类'public class CardsActivity extends FragmentActivity '

以上是关于android:如何修复类 CardsActivity.CardPagerAdapter 中的错误构造函数 CardPagerAdapter 不能应用于给定类型的主要内容,如果未能解决你的问题,请参考以下文章

Android热修复基础篇

Android热修复:以DexClassLoader类加载原理编写demo实现类替换修复

Android热修复:以DexClassLoader类加载原理编写demo实现类替换修复

如何在android java中修复sqliteException

如何在 ROOM android 中修复“不确定如何将光标转换为此方法的返回类型”

Android 使用类加载器原理实现热修复