content必须有一个ListView,其id属性为'android.R.id.list'

时间:2014-02-13 10:37:16

标签: android listview exception android-listview

02-13 05:29:12.454: E/AndroidRuntime(1374): FATAL EXCEPTION: main
02-13 05:29:12.454: E/AndroidRuntime(1374): Process: net.multiplesystem.nosms, PID: 1374
02-13 05:29:12.454: E/AndroidRuntime(1374): java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
02-13 05:29:12.454: E/AndroidRuntime(1374):     at android.support.v4.app.ListFragment.ensureList(ListFragment.java:344)
02-13 05:29:12.454: E/AndroidRuntime(1374):     at android.support.v4.app.ListFragment.onViewCreated(ListFragment.java:145)
02-13 05:29:12.454: E/AndroidRuntime(1374):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:941)
02-13 05:29:12.454: E/AndroidRuntime(1374):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
02-13 05:29:12.454: E/AndroidRuntime(1374):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
02-13 05:29:12.454: E/AndroidRuntime(1374):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
02-13 05:29:12.454: E/AndroidRuntime(1374):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
02-13 05:29:12.454: E/AndroidRuntime(1374):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
02-13 05:29:12.454: E/AndroidRuntime(1374):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
02-13 05:29:12.454: E/AndroidRuntime(1374):     at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
02-13 05:29:12.454: E/AndroidRuntime(1374):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
02-13 05:29:12.454: E/AndroidRuntime(1374):     at android.view.View.measure(View.java:16458)
02-13 05:29:12.454: E/AndroidRuntime(1374):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
02-13 05:29:12.454: E/AndroidRuntime(1374):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-13 05:29:12.454: E/AndroidRuntime(1374):     at android.view.View.measure(View.java:16458)
02-13 05:29:12.454: E/AndroidRuntime(1374):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
02-13 05:29:12.454: E/AndroidRuntime(1374):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
02-13 05:29:12.454: E/AndroidRuntime(1374):     at android.view.View.measure(View.java:16458)
02-13 05:29:12.454: E/AndroidRuntime(1374):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
02-13 05:29:12.454: E/AndroidRuntime(1374):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-13 05:29:12.454: E/AndroidRuntime(1374):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2289)
02-13 05:29:12.454: E/AndroidRuntime(1374):     at android.view.View.measure(View.java:16458)

XML

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent" />
</LinearLayout>

JAVA

public class UserDetails extends ListFragment  {
    ArrayAdapter<String> adapter;
    ArrayList<String> userIndex = new ArrayList<String>();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View android = inflater.inflate(R.layout.users_tab, container, false);   
        adapter = new ArrayAdapter<String>(inflater.getContext(),R.layout.single_user_chat_tab,userIndex);
        setListAdapter(adapter);
        return android;
    }
}

当我执行程序时,它显示运行时异常。但是我已经准备好了有id列表的listview。我不知道为什么它会显示异常。请指导我的朋友为什么抛出异常。我已经尝试了所有解决方案,但仍然遇到同样的问题。

2 个答案:

答案 0 :(得分:1)

我不认为你可以在onCreateView中返回自定义视图之前调用setListAdapter。我看到3个解决方案

  1. 调用super.onCreateView()而不是膨胀
  2. 在OnActivityCreated中设置适配器
  3. 写android.findViewById(android.R.id.list).setAdapter(adapter);代替

答案 1 :(得分:0)

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