如何修复这个? >>与FragmentStatePagerAdapter不兼容

时间:2014-04-27 06:21:58

标签: android android-fragments android-listview fragment android-fragmentactivity

我有一个程序使用片段,但我有一个问题,当我在我的寻呼机中包含列表视图时,选择滑动菜单上的一个项目。

这些错误:

标签适配器

上的

public class TabsPagerAdapter扩展了FragmentStatePagerAdapter {

public TabsPagerAdapter(android.support.v4.app.FragmentManager fragmentManager) {
    super(fragmentManager);
}

@Override
public FragmentActivity getItem(int index) {

    switch (index) {
    case 0:
        return new SoloAttractionFragment();
    case 1:
        return new KaranganyarAttractionFragment();
    case 2:
        return new SukoharjoAttractionFragment();
    }

    return null;
}

@Override
public int getCount() {
    // get item count - equal to number of tabs
    return 3;
}

}

行中的错误:

public FragmentActivity getItem(int index){

此行有多个标记      - 工具      android.support.v4.app.FragmentStatePagerAdapter.getItem      - 返回类型与。不兼容      FragmentStatePagerAdapter.getItem(INT)      - 返回类型与。不兼容      FragmentStatePagerAdapter.getItem(INT)

请帮助我,谢谢

其他相关课程:

public class SoloAttractionFragment扩展了FragmentActivity {

// All static variables
static final String URL = "http://api.androidhive.info/music/music.xml";
// XML node keys
public static final String KEY_SONG = "song"; // parent node
public static final String KEY_ID = "id";
public static final String KEY_TITLE = "title";
public static final String KEY_ARTIST = "artist";
public static final String KEY_DURATION = "duration";
public static final String KEY_THUMB_URL = "thumb_url";

ListView list;
LazyAdapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_attraction_solo);

    ArrayList<HashMap<String, String>> songsList = new 
            ArrayList<HashMap<String, String>>();

    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL); // getting XML from URL
    Document doc = parser.getDomElement(xml); // getting DOM element

    NodeList nl = doc.getElementsByTagName(KEY_SONG);
    // looping through all song nodes &lt;song&gt;
    for (int i = 0; i < nl.getLength(); i++) {
        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);
        // adding each child node to HashMap key =&gt; value
        map.put(KEY_ID, parser.getValue(e, KEY_ID));
        map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
        map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
        map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
        map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

        // adding HashList to ArrayList
        songsList.add(map);
    }

    list=(ListView)findViewById(R.id.list);

    // Getting adapter by passing xml data ArrayList
    adapter=new LazyAdapter(this, songsList);
    list.setAdapter(adapter);

    // Click event for single list row
    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            // TODO Auto-generated method stub
        }
        });
}

这是列表视图的适配器:

public class LazyAdapter扩展了BaseAdapter {

private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader; 

public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoader(activity.getApplicationContext());
}

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

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.list_row, null);

    TextView title = (TextView)vi.findViewById(R.id.title); // title
    TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
    TextView duration = (TextView)vi.findViewById(R.id.duration); // duration
    ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image

    HashMap<String, String> song = new HashMap<String, String>();
    song = data.get(position);

    // Setting all values in listview
    title.setText(song.get(SoloAttractionFragment.KEY_TITLE));
    artist.setText(song.get(SoloAttractionFragment.KEY_ARTIST));
    duration.setText(song.get(SoloAttractionFragment.KEY_DURATION));
    imageLoader.DisplayImage(song.get(SoloAttractionFragment.KEY_THUMB_URL), position, thumb_image);
    return vi;
}

}

2 个答案:

答案 0 :(得分:1)

public class SoloAttractionFragment extends android.support.v4.app.Fragment

答案 1 :(得分:0)

您无法从getItem()返回FragmentActivity,因为FragmentActivity不是片段,而是可以包含片段的Activity。 getItem()必须返回Fragment或ListFragment。