setAdapter“调用需要 API 级别 11(当前最小值为 8):android.widget.AbsListView#setAdapter”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12545462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:56:13  来源:igfitidea点击:

setAdapter "Call requires API level 11 (current min is 8): android.widget.AbsListView#setAdapter"?

androidandroid-fragments

提问by Enigman

I'm calling the setAdapter()method in a class that extends Fragment. Note that I have imported android.support.v4.app.Fragment.

setAdapter()在一个扩展的类中调用该方法Fragment。请注意,我已经导入了android.support.v4.app.Fragment.

However I get an error stating that the API level is required to be level 11.

但是,我收到一条错误消息,指出 API 级别必须为 11 级。

What do I have to do so that I can fix this without changing minSdkVersion="8"to minSdkVersion="11"

我该怎么做才能在不更改minSdkVersion="8"为的情况下解决此问题minSdkVersion="11"

package foo.bar.qux;

import java.util.Calendar;
import java.util.Date;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import android.support.v4.app.Fragment;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class TabFragmentList extends Fragment {

    String category, xml;
    NodeList nodes;
    int numResults;
    private ListView lv;
    Date date;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        if (container == null) return null;
        return (LinearLayout) inflater.inflate(R.layout.eventlist, container,
                false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Activity activity = getActivity();
        if (activity != null) {
            final Calendar c = Calendar.getInstance();
            ListContent[] item = new ListContent[3];
            category = (String) activity.getIntent().getExtras()
                    .get("category");
            xml = (String) activity.getIntent().getExtras().get("xml");
            TextView tv = (TextView) getView().findViewById(R.id.category);
            tv.setText(category);
            nodes = doc.getElementsByTagName("event");
            for (int i = 0; i < 3; i++) {
                c.add(Calendar.DATE, 1);
                date = c.getTime();
                item[i] = new ListContent();
                item[i].setList(nodes, category, date);
            }
            EventListAdapter adapter = new EventListAdapter(activity,
                    R.layout.eventlist_row, item);
            lv = (ListView) getView().findViewById(R.id.listView1);
            lv.setAdapter(adapter); // ERROR SHOWN HERE
        }
    }
}

Note : I tried reducing the targetSdkVersion to 10. Yet I get the error. Please help!

注意:我尝试将 targetSdkVersion 减少到 10。但是我得到了错误。请帮忙!

Edit : I don't understand why it's termed as AbsListVewwhen all that I have used is a ListView. Also, note that I've used a custom adapter.

编辑:我不明白为什么它被称为AbsListVew当我使用的所有内容都是ListView. 另请注意,我使用了自定义适配器。

EventListAdapterextends ArrayAdaptere<ListContent>and shows NO ERROR.

EventListAdapter扩展ArrayAdaptere<ListContent>并显示没有错误。

For your reference here is the xml layout code snippet for R.id.ListView1

供您参考这里是 R.id.ListView1 的 xml 布局代码片段

<ListView
        android:layout_margin="10dp"
        android:dividerHeight="10.0sp"
        android:id="@+id/listView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

回答by Jainendra

Right click on the project folder > Android tools > Clear Link Markers

Right click on the project folder > Android tools > Clear Link Markers

"Run Android Lint" makes some markers and the markers cause this error.

“运行 Android Lint”会生成一些标记,这些标记会导致此错误。

回答by Renetik

Solution is just casting type to correct subtype as later Android versions introduced method call with same name.

解决方案只是将类型转换为正确的子类型,因为后来的 Android 版本引入了同名的方法调用。

@SuppressWarnings({ "unchecked", "rawtypes" })

EventListAdapter adapter = new EventListAdapter(activity, R.layout.eventlist_row, item);
lv = (ListView) getView().findViewById(R.id.listView1);
((AdapterView)lv).setAdapter(adapter); //ERROR SOLVED HERE

回答by ArloGrant

I solved the "abstract method not implemented error" by casting my setAdapter method like this:

我通过像这样转换我的 setAdapter 方法解决了“未实现抽象方法的错误”:

 @SuppressWarnings("unchecked")
 public void setAdapterSDK8(Adapter adapter) {
    ((AdapterView) this).setAdapter(adapter);
    ((StaggeredGridView) this).setAdapter((ListAdapter) adapter);
 }

This is related to the etsy staggered gridview

这与 etsy 交错网格视图有关

回答by Ovidiu Latcu

As you can see in the docs setAdapter()method on AbsListViewis available just from API Level 11. So there isn't much you can do, you can just cast eventually your AbsListViewto a ListViewor GridViewand then call setAdapter().

正如您在 docssetAdapter()方法中所见,AbsListView仅从 API 级别 11 可用。因此您无能为力,您可以最终将您的转换AbsListView为 a ListVieworGridView然后调用setAdapter().