gpt4 book ai didi

android - 如何将数据加载到 ListFragment?

转载 作者:行者123 更新时间:2023-11-30 02:54:38 28 4
gpt4 key购买 nike

我正在尝试将一些数据加载到 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'

我该如何处理?

这是我的文件,如果它们可以帮助的话:

主 Activity .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>

提前致谢!

最佳答案

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 检查布局中的 ListView ID。如果 View 为 null,它将抛出 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>

关于android - 如何将数据加载到 ListFragment?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23506738/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com