java 如何将列表视图放在android中的片段中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11564447/
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-10-31 05:35:49  来源:igfitidea点击:

How to put a listview inside of a fragment in android?

javaandroidlistviewfragment

提问by jacosta

I'm trying to put a simple listview inside my fragment. I'm getting an error when I run it as is. I wasn't expecting it to work with the current code I have, but I'm not sure where to go from here. Any help would be greatly appreciated!

我试图在我的片段中放入一个简单的列表视图。按原样运行时出现错误。我没想到它可以与我现有的代码一起使用,但我不确定从哪里开始。任何帮助将不胜感激!

My code:

我的代码:

public class Tab1Fragment extends ListFragment {

ListView listView;

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

    LinearLayout theLayout = (LinearLayout) inflater.inflate(R.layout.tab1, container, false);
    listView = (ListView)theLayout.findViewById(R.id.ListView01);
    return theLayout;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Use an existing ListAdapter that will map an array
    // of strings to TextViews
    setListAdapter(new ArrayAdapter<String>(getActivity().getApplicationContext(),
            android.R.layout.simple_list_item_1, mStrings));
    getListView().setTextFilterEnabled(true);
}

private String[] mStrings = {
        "Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama",
        "Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller"
    };

}

}

my runtime error:

我的运行时错误:

07-19 11:42:45.214: E/AndroidRuntime(19873): FATAL EXCEPTION: main
07-19 11:42:45.214: E/AndroidRuntime(19873): java.lang.RuntimeException: Unable to start activity ComponentInfo{package/package.TabActionBarActivity}: java.lang.IllegalStateException: Content view not yet created
07-19 11:42:45.214: E/AndroidRuntime(19873):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
07-19 11:42:45.214: E/AndroidRuntime(19873):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-19 11:42:45.214: E/AndroidRuntime(19873):    at android.app.ActivityThread.access0(ActivityThread.java:123)
07-19 11:42:45.214: E/AndroidRuntime(19873):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)

回答by trevor-e

You should try calling setListAdapter()in the onActivityCreated()method. This is because the Activity hasn't been completely created by the onCreate()method of a Fragment since the lifecycles of each are slightly different.

您应该尝试调用setListAdapter()onActivityCreated()方法。这是因为 Activity 还没有完全由onCreate()Fragment的方法创建,因为每个的生命周期略有不同。

Here is a similar question: Android Fragment onCreateView vs. onActivityCreated

这是一个类似的问题:Android Fragment onCreateView vs. onActivityCreated

回答by azgolfer

A couple of problems in your fragment:

您的片段中有几个问题:

  1. If you are using ListFragment, then in your XML layout, you must have a ListView that has id of 'android.R.id.list'.
  2. Call your setListAdapter() method in onViewCreated() instead of onCreate(). This is because onCreate() is called first before onCreateView().
  1. 如果您使用的是 ListFragment,那么在您的 XML 布局中,您必须有一个 ID 为“android.R.id.list”的 ListView。
  2. 在 onViewCreated() 而不是 onCreate() 中调用 setListAdapter() 方法。这是因为 onCreate() 在 onCreateView() 之前先被调用。