qq_40821173
qq_40821173
采纳率0%
2018-04-22 16:39 阅读 3.5k

Android studio报错:无法解析v4.app.FragmentActivity

10

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.example.administrator.cookbook.R;
import com.example.administrator.cookbook.books.CookBookPresenter;
import com.example.administrator.cookbook.books.CookBookPresenterImpl;
import com.example.administrator.cookbook.books.CookBookView;
import com.example.administrator.cookbook.cookbook.beans.CookBook;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment implements CookBookView{

ConvenientBanner convenientBanner;
ListView listView;
static List<Integer> localImages = new ArrayList<>();
CookBookPresenter cookBookPresenter;
List<CookBook> list;

static {
    localImages.add(R.drawable.test01);
    localImages.add(R.drawable.test02);
    localImages.add(R.drawable.test03);
    localImages.add(R.drawable.test04);
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    list = new ArrayList<>();
    cookBookPresenter = new CookBookPresenterImpl(this);
    cookBookPresenter.getCookBookListById(getActivity(),"xxx");
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_home,container,false);
    initViews(view);
    initBanner();
    return view;
}

void initViews(View view){
    listView = (ListView) view.findViewById(R.id.listview);
    convenientBanner = (ConvenientBanner)view.findViewById(R.id.convenientBanner);
}

@Override
public void setCookBookList(List<CookBook> list) {
    this.list = list;
}

@Override
public void setFail() {

}

class MyAdapter extends BaseAdapter{

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View myView;
        if(convertView == null){
            myView = getActivity().getLayoutInflater().inflate(R.layout.list_cookbook_item,null);
        }else {
            myView = convertView;
        }
        ImageView iv = (ImageView) myView.findViewById(R.id.imageView);
        TextView tv1 = (TextView) myView.findViewById(R.id.title);
        TextView tv2 = (TextView) myView.findViewById(R.id.intro);

        CookBook cookBook = list.get(position);

        String url = cookBook.getAlbums().get(0);
        String title = cookBook.getTitle();
        String intro = cookBook.getImtro();

        tv1.setText(title);
        tv2.setText(intro);

        Picasso.with(getActivity()).load(url).resize(120,120).into(iv);

        return myView;
    }
}

void initBanner(){
    convenientBanner.setPages(
            new CBViewHolderCreator<LocalImageHolderView>() {
                @Override
                public LocalImageHolderView createHolder() {
                    return new LocalImageHolderView();
                }
            }, localImages)
            //设置两个点图片作为翻页指示器,不设置则没有指示器,可以根据自己需求自行配合自己的指示器,不需要圆点指示器可用不设
            .setPageIndicator(new int[]{R.drawable.ic_page_indicator, R.drawable.ic_page_indicator_focused})
            //设置指示器的方向
            .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.ALIGN_PARENT_RIGHT);
}

class LocalImageHolderView implements Holder<Integer> {
    private ImageView imageView;
    @Override
    public View createView(Context context) {
        imageView = new ImageView(context);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        return imageView;
    }

    @Override
    public void UpdateUI(Context context, final int position, Integer data) {
        imageView.setImageResource(data);
    }
}

}
其中Picasso.with(getActivity()).load(url).resize(120,120).into(iv);中with报错Cannot resolve method 'with(android.support.v4.app.FragmentActivity),该如何解决,本人小白能否给出详细过程

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

5条回答 默认 最新

  • z979451341 键盘舞者113 2018-04-23 01:07

    把Fragment依附的Activity换成继承FragmentActivity或AppCompatActivity

    点赞 评论 复制链接分享
  • qq_19566795 qq_19566795 2018-04-23 02:38

    1.让其继承FragmentActivity
    2.在主程序的文件夹下新建一个叫做libs的文件夹,将其它项目的android-support-v4.jar放入,再次导入

    点赞 评论 复制链接分享
  • qq_33756493 昵称_Justin 2018-04-23 13:28

    在adapter中定一个Context,然后在创建有参adapter的时候传一个context进去

    点赞 评论 复制链接分享
  • e_ron e_ron 2018-04-23 14:50

    检查v4和v7版本试试

    点赞 评论 复制链接分享
  • qq_41614845 qq_41614845 2018-04-25 05:23

    用到FragmentActivity,要导入Android.support.v4.app.FragmentActivity包,Fragment,FragmentManager,FragmentTransaction都要用的v4包,

    Activity要继承FragmentActivity

    点赞 评论 复制链接分享

相关推荐