方法需要对象并从扩展类中查找对象

时间:2015-07-04 00:31:42

标签: java android android-fragments

我想在我的Android应用活动中创建片段,我可以轻扫。更具体地说,这些片段包含图像,我想浏览这些片段。

现在要完成这个,我使用了FragmentPagerAdapter。我现在遇到的问题是我在扩展FragmentPagerAdaptor的类中的一个方法需要返回一个Fragment对象,但根据编译器它会找到一个不同的对象。 这很奇怪,因为我返回的对象来自扩展Fragment的类,因此它应该在我看来是有用的。

问题的方法是getItem(int position)SlidesPageAdaptor,错误是

Incompatible types
Required: android.support.v4.app.Fragment
Found: ourpackagename.fragment_slides

SlidesPageAdaptor.java

import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class SlidesPageAdaptor extends FragmentPagerAdapter {

String[] devices;
String[] deviceDescription;

public SlidesPageAdaptor(FragmentManager fm, Context context) {
    super(fm);

    Resources resources = context.getResources();

    devices = resources.getStringArray(R.array.devices);
    deviceDescription =        resources.getStringArray(R.array.device_description);
}

@Override
public Fragment getItem(int position) {

    Bundle bundle = new Bundle();
    bundle.putString(fragment_slides.DescriptionKey, deviceDescription[position]);
    bundle.putInt(fragment_slides.ImageIDKey, getImageID(position));

    fragment_slides fragmentSlide = new fragment_slides();
    fragmentSlide.setArguments(bundle);

    return fragmentSlide;
}

private int getImageID(int position) {

    int id = 0;
    switch (position)
    {
        case 0:
            id = R.drawable.abc_btn_check_material;
            break;
        case 1:
            id = R.drawable.abc_ab_share_pack_mtrl_alpha;
            break;
        case 2:
            id = R.drawable.abc_btn_radio_material;
            break;
    }

    return id;
}

@Override
public CharSequence getPageTitle(int position) {
    return devices[position];
}

@Override
public int getCount() {
    return devices.length;
}

}

fragment_slides.class(我知道名字不好,但现在不重要):

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class fragment_slides extends Fragment {

public static final String ImageIDKey = "imagekey";
public static final String DescriptionKey = "descriptionkey";

    public fragment_slides() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

   // Inflate the layout for this fragment

    View view = inflater.inflate(R.layout.fragment_slides_images, container, false);

    Bundle bundle = getArguments();

    if(bundle != null)
    {
        int imageID = bundle.getInt(ImageIDKey);
        String description = bundle.getString(DescriptionKey);

        setValues(view, imageID, description);
    }

    return view;

    //return inflater.inflate(R.layout.fragment_slides_images, container, false);
}

private void setValues(View view, int imageID, String description) {
    ImageView imageview = (ImageView) view.findViewById(R.id.imageViewSlide);
    imageview.setImageResource(imageID);

    TextView textView = (TextView) view.findViewById(R.id.tvSlideTest);
    textView.setText(description);
}

}

我认为不需要其他活动/课程,但如果需要,我可以提供。

2 个答案:

答案 0 :(得分:2)

fragment_slides.class中,您应该导入android.support.v4.app.Fragment,而不是android.app.Fragment

答案 1 :(得分:0)

问题在于:

fragment_slides fragmentSlide = new fragment_slides();
fragmentSlide.setArguments(bundle);

由于您声明的是fragment_slides对象而不是Fragment对象,因此编译器认为它不是Fragment。试试这个:

Fragment fragmentSlide = new fragment_slides();
fragmentSlide.setArguments(bundle);
相关问题