本文介绍了一个片段里面的ListView给出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个片段里面显示的ListView的问题。我用我的片段ListFragment。我能不能找出在我的实际问题。
CategoriesFragment.java
公共类CategoriesFragment扩展ListFragment {
LV的ListView;
TextView的电视;
公共CategoriesFragment(){
} @覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){
如果(集装箱== NULL)
返回null;
。inflater.getContext()getSystemService(Context.LAYOUT_INFLATER_SERVICE);
查看查看= inflater.inflate(R.layout.categories_fragment,集装箱,FALSE);
查看的ListView =(ListView控件)view.findViewById(R.id.listview1);
返回视图。
} @覆盖
公共无效onActivityCreated(捆绑savedInstanceState){
super.onActivityCreated(savedInstanceState);
最终的String [] =水果新的String [] {苹果,鳄梨,香蕉,
蓝莓,椰子,榴莲,番石榴,猕猴桃};
setListAdapter(新ArrayAdapter<串GT;
(getActivity(),R.layout.categories_fragment,水果));
}
}
PagerAdaper.java
公共类SectionsPagerAdapter扩展FragmentPagerAdapter {
公共SectionsPagerAdapter(FragmentManager FM){
超(FM);
} @覆盖
公共片段的getItem(int i)以{
开关(ⅰ){
情况下0:
片段片段=新CategoriesFragment();
返回片段;
打破;
情况1:
片段片段=新FeaturedFragment();
返回片段;
打破
}
返回null;
} @覆盖
公众诠释的getCount(){
返回2;
} @覆盖
公共CharSequence的getPageTitle(INT位置){
开关(位置){
情况下0:回归类别;
案例1:回归精选;
}
返回null;
}
}
categories_fragment.xml
<?XML版本=1.0编码=UTF-8&GT?;< ListView控件的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / ListView1的
机器人:layout_width =match_parent
机器人:layout_height =match_parent/>
DDMS错误文本:
12月9日至6日:37:47.402:E / AndroidRuntime(2611):致命异常:主要
12月9日至6日:37:47.402:E / AndroidRuntime(2611):了java.lang.RuntimeException:您的内容必须有一个ListView的id属性是'android.R.id.list
12月9日至6日:37:47.402:E / AndroidRuntime(2611):在android.support.v4.app.ListFragment.ensureList(ListFragment.java:344)
12月9日至6日:37:47.402:E / AndroidRuntime(2611):在android.support.v4.app.ListFragment.onViewCreated(ListFragment.java:145)
12月9日至6日:37:47.402:E / AndroidRuntime(2611):在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
12月9日至6日:37:47.402:E / AndroidRuntime(2611):在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
12月9日至6日:37:47.402:E / AndroidRuntime(2611):在android.support.v4.app.BackStackRecord.run(BackStackRecord.java:635)
12月9日至6日:37:47.402:E / AndroidRuntime(2611):在android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431)
12月9日至6日:37:47.402:E / AndroidRuntime(2611):在android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:431)
12月9日至6日:37:47.402:E / AndroidRuntime(2611):在android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
12月9日至6日:37:47.402:E / AndroidRuntime(2611):在android.support.v4.view.ViewPager.populate(ViewPager.java:895)
12月9日至6日:37:47.402:E / AndroidRuntime(2611):在android.support.v4.view.ViewPager.populate(ViewPager.java:772)
12月9日至6日:37:47.402:E / AndroidRuntime(2611):在android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1234)
12月9日至6日:37:47.402:E / AndroidRuntime(2611):在android.view.View.measure(View.java:12723)
12月9日至6日:37:47.402:E / AndroidRuntime(2611):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
12月9日至6日:37:47.402:E / AndroidRuntime(2611):在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
12月9日至6日:37:47.402:E / AndroidRuntime(2611):在android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1017)
12月9日至6日:37:47.402:E / AndroidRuntime(2611):在android.widget.LinearLayout.onMeasure(LinearLayout.java:555)
12月9日至6日:37:47.402:E / AndroidRuntime(2611):在android.view.View.measure(View.java:12723)
12月9日至6日:37:47.402:E / AndroidRuntime(2611):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
12月9日至6日:37:47.402:E / AndroidRuntime(2611):在android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
12月9日至6日:37:47.402:E / AndroidRuntime(2611):在android.view.View.measure(View.java:12723)
12月9日至6日:37:47.402:E / AndroidRuntime(2611):在android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
12月9日至6日:37:47.402:E / AndroidRuntime(2611):在android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
12月9日至6日:37:47.402:E / AndroidRuntime(2611):在android.view.View.measure(View.java:12723)
12月9日至6日:37:47.402:E / AndroidRuntime(2611):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
12月9日至6日:37:47.402:E / AndroidRuntime(2611):在android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
解决方案
更改此
< ListView控件的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / ListView1的
机器人:layout_width =match_parent
机器人:layout_height =match_parent/>
到
< ListView控件的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@机器人:ID /列表
机器人:layout_width =match_parent
机器人:layout_height =match_parent/>
问题是ID。当你扩展的 ListFragment 您的ListView必须有一些ID这样的, @android:ID /列表
I have issues in displaying ListView inside a Fragment. I am using ListFragment in my Fragment. I couldn't able to figure out where my actual issue is.
CategoriesFragment.java
public class CategoriesFragment extends ListFragment {
ListView lv;
TextView tv;
public CategoriesFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(container==null)
return null;
inflater.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.categories_fragment,container,false);
View listView = (ListView)view.findViewById(R.id.listview1);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final String[] fruits = new String[] { "Apple", "Avocado", "Banana",
"Blueberry", "Coconut", "Durian", "Guava", "Kiwifruit"};
setListAdapter(new ArrayAdapter<String>
(getActivity(),R.layout.categories_fragment,fruits));
}
}
PagerAdaper.java
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
switch(i) {
case 0:
Fragment fragment = new CategoriesFragment();
return fragment;
break;
case 1:
Fragment fragment = new FeaturedFragment();
return fragment;
break
}
return null;
}
@Override
public int getCount() {
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0: return "CATEGORIES";
case 1: return "FEATURED";
}
return null;
}
}
categories_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listview1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
DDMS error text:
09-06 12:37:47.402: E/AndroidRuntime(2611): FATAL EXCEPTION: main
09-06 12:37:47.402: E/AndroidRuntime(2611): java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.support.v4.app.ListFragment.ensureList(ListFragment.java:344)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.support.v4.app.ListFragment.onViewCreated(ListFragment.java:145)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:635)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:431)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.support.v4.view.ViewPager.populate(ViewPager.java:895)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.support.v4.view.ViewPager.populate(ViewPager.java:772)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1234)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.view.View.measure(View.java:12723)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1017)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.widget.LinearLayout.onMeasure(LinearLayout.java:555)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.view.View.measure(View.java:12723)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.view.View.measure(View.java:12723)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.view.View.measure(View.java:12723)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
09-06 12:37:47.402: E/AndroidRuntime(2611): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
解决方案
Change this
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listview1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
to,
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
The problem is with the ID. When you extend ListFragment your ListView must have id something like this, @android:id/list
这篇关于一个片段里面的ListView给出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!