为什么onScrollListener认为没有创建ListView?

时间:2013-11-09 19:38:52

标签: android android-listview

因此引发的错误是

java.lang.IllegalStateException: Content view not yet created

当我取消注释getListView().setOnScrollListener(this);行时,会抛出此内容。

public class MyCategoryFragment extends ListFragment implements OnScrollListener {
    private ArrayList<Article> m_articles = new ArrayList<Article>();
    public ArticleAdapter m_artadapter;


    public MyCategoryFragment() {
        super();
    }

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

        m_articles = manager.loadCategory(getActionBar().getTitle().toString(), 10);
        m_artadapter = new ArticleAdapter(this.getActivity(), R.layout.article_button, m_articles);
        this.setListAdapter(m_artadapter);

        //getListView().setOnScrollListener(this);
        return rootView;
    }

这是布局,因此您可以看到它是正确的列表视图

<?xml version="1.0" encoding="utf-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >
    <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            />
    <TextView
            android:id="@android:id/empty"
            android:textColor="#FFFFFF"
            android:textSize="30sp"
            android:gravity="center_vertical|center_horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="It's not you it's us..."/>
</LinearLayout>

我已经对此进行了广泛的研究,我相信必须要这么简单,但需要一天多的时间来解决这个问题。感谢

1 个答案:

答案 0 :(得分:2)

因为onCreateView文档保持不变:

创建并返回与片段关联的视图层次结构 因此,由于该方法不返回,您将无法通过getListView()访问ListView。您可以在OnActivityCreate回调中获取有效的引用。