【问题标题】:How to load data to a ListFragment?如何将数据加载到 ListFragment?
【发布时间】:2014-05-07 00:28:31
【问题描述】:

我正在尝试将一些数据加载到 ListFragment 中,但出现此错误:

05-07 00:05:30.533   625-625/com.myapp.android E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

我该如何处理?

这是我的文件,如果他们能提供帮助的话:

MainActivity.java

public class MainActivity extends FragmentActivity {

private ViewPager viewPager;
private ViewpagerAdapter viewpagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    viewPager = (ViewPager) findViewById(R.id.pager);
    viewpagerAdapter = new ViewpagerAdapter(getSupportFragmentManager());
    viewPager.setAdapter(viewpagerAdapter);

}

}

ListFrag.java

public class ListFrag extends ListFragment {

private DatabaseAdapter databaseAdapter;

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

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

}

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

    databaseAdapter = new DatabaseAdapter(getActivity());
    databaseAdapter.open();

    List<Foo> values = databaseAdapter.getAllFoobar();

    //ArrayAdapter<Foo> adapter = new ArrayAdapter<Foo>(getActivity(), android.R.layout.simple_list_item_1, values);
    //setListAdapter(adapter);

}

}

和fragment.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.myapp.android.ListFrag">

<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></ListView>

</FrameLayout>

提前致谢!

【问题讨论】:

    标签: android android-fragments android-listview android-sqlite android-listfragment


    【解决方案1】:
    java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
    

    崩溃日志清楚地说明了问题。您需要更改 ListView id 。 ListFragment 期望布局具有 ListView id 为 @android:id/list

     android:id="@android:id/list"
    

    如果您查看ListFragment 源代码,它会使用android.R.id.list ID 检查布局中的ListView。如果视图为空,它会抛出一个RuntimeException

          View rawListView = root.findViewById(android.R.id.list);
            if (!(rawListView instanceof ListView)) {
                if (rawListView == null) {
                    throw new RuntimeException(
                            "Your content must have a ListView whose id attribute is " +
                            "'android.R.id.list'");
                }
                throw new RuntimeException(
                        "Content has view with id attribute 'android.R.id.list' "
                        + "that is not a ListView class");
            }
    

    使用这个布局

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical" >
    
    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多