我收到错误“您的内容必须有一个ListView,其id属性为'android.R.id.list”。我尝试了所有的东西,但仍然无法正常工作。谢谢你的帮助!
如你所见,我没有做任何其他事情。但我得到了错误:
您的内容必须有一个ListView,其id属性为'android.R.id.list' 即使我的xml中有android:id =“@ + id / list”行。
ListFragment文件
public class MmFragment extends ListFragment implements AdapterView.OnItemClickListener {
String[] datasource={"English","French","Khmer","Japanese","Russian","Chinese"};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.MmFragment_layout, container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),R.layout.MmFragment _layout,R.id.txtitem,datasource);
// Bind adapter to the ListFragment
setListAdapter(adapter);
// Retain the ListFragment instance across Activity re-creation
setRetainInstance(true);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ViewGroup viewg=(ViewGroup)view;
TextView tv = (TextView)viewg.findViewById(R.id.txtitem);
Toast.makeText(getActivity(), tv.getText().toString(),Toast.LENGTH_LONG).show();
}
}
XML的文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/txtitem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
/>
<ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
</LinearLayout>
错误代码
java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
at android.support.v4.app.ListFragment.ensureList(ListFragment.java:344)
at android.support.v4.app.ListFragment.onViewCreated(ListFragment.java:145)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1197)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1562)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:535)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
at android.support.v4.view.ViewPager$3.run(ViewPager.java:251)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:603)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5422)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:1)
使用
android:id="@android:id/list"
而不是
android:id="@id/android:list"
答案 1 :(得分:0)
试试这个
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/txtitem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
/>
<ListView android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
</LinearLayout>
答案 2 :(得分:0)
使用android:id =&#34; @ + android:id / list&#34;