内容必须具有ListView,其id属性为'android.R.id.list'

时间:2014-02-17 23:55:41

标签: android listview android-listfragment

我的应用程序编译并运行,但是当我尝试从它启动的选项卡更改为任何其他选项卡(其他选项卡中只有一个包含列表)时崩溃。我搜索了这里发布的类似问题,但找不到有效的解决方案。

如果您需要查看此处未发布的代码或ToDoListFragment的完整代码,整个项目将位于https://github.com/BathUniApp/bath-uni-app。以下是最相关的位:

Logcat输出:

E/AndroidRuntime(15409): FATAL EXCEPTION: main
E/AndroidRuntime(15409): java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
E/AndroidRuntime(15409):        at android.support.v4.app.ListFragment.ensureList(ListFragment.java:344)
E/AndroidRuntime(15409):        at android.support.v4.app.ListFragment.onViewCreated(ListFragment.java:145)
E/AndroidRuntime(15409):        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:941)
E/AndroidRuntime(15409):        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
E/AndroidRuntime(15409):        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
E/AndroidRuntime(15409):        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
E/AndroidRuntime(15409):        at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
E/AndroidRuntime(15409):        at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
E/AndroidRuntime(15409):        at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
E/AndroidRuntime(15409):        at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:550)
E/AndroidRuntime(15409):        at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:509)
E/AndroidRuntime(15409):        at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:490)
E/AndroidRuntime(15409):        at ip7.bathuniapp.MainActivity.onTabSelected(MainActivity.java:51)
E/AndroidRuntime(15409):        at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:570)
E/AndroidRuntime(15409):        at com.android.internal.app.ActionBarImpl$TabImpl.select(ActionBarImpl.java:1067)
E/AndroidRuntime(15409):        at com.android.internal.widget.ScrollingTabContainerView$TabClickListener.onClick(ScrollingTabContainerView.java:489)
E/AndroidRuntime(15409):        at android.view.View.performClick(View.java:4106)
E/AndroidRuntime(15409):        at android.view.View$PerformClick.run(View.java:17150)
E/AndroidRuntime(15409):        at android.os.Handler.handleCallback(Handler.java:615)
E/AndroidRuntime(15409):        at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(15409):        at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(15409):        at android.app.ActivityThread.main(ActivityThread.java:4793)
E/AndroidRuntime(15409):        at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(15409):        at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(15409):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:808)
E/AndroidRuntime(15409):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575)
E/AndroidRuntime(15409):        at dalvik.system.NativeStart.main(Native Method)

MainActivity.java:

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {

private NonSwipeableViewPager nsViewPager;
private TabsPagerAdapter tabsAdapter;
private ActionBar actionBar;
private String[] tabs = {"Settings", "Buses", "Classes",
                         "To-Do list", "Maps" };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    nsViewPager = (NonSwipeableViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    tabsAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    nsViewPager.setAdapter(tabsAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    //add the tabs
    for (String name : tabs) {
        actionBar.addTab(actionBar.newTab()
                   .setText(name)
                   .setTabListener(this));
    }
}

@Override
public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) { }

@Override
public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {

    //the line mentioned in logcat
    nsViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) { }

}

TabsPagerAdapter.java(因为它在崩溃时更改标签,我认为这可能是问题,但我看不出如何。):

public class TabsPagerAdapter extends FragmentPagerAdapter {
    public TabsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int index) {
        switch (index) {
        case 0:
            return new SettingsFragment();
        case 1:
            return new BusesFragment();
        case 2:
            return new ClassesFragment();
        case 3:
            return new ToDoListFragment();
        case 4:
            return new MapsFragment();
        }
        return null;
    }

    @Override
    public int getCount() {
        // item count, equal to number of tabs
        return 5;
    }
}

带有列表的选项卡的相关部分,ToDoListFragment.java(此处为完整代码:https://github.com/BathUniApp/bath-uni-app/blob/master/src/ip7/bathuniapp/ToDoListFragment.java):

public class ToDoListFragment extends ListFragment implements OnClickListener {
private TasksDataSource datasource;
private ArrayList<ListItem> todolist = new ArrayList<ListItem>();

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

    View v = inflater.inflate(R.layout.frag_todolist, null);
    ListView listView = (ListView) getView().findViewById(android.R.id.list);

    // Get the datasource for Tasks
    datasource = new TasksDataSource(this.getActivity());
    datasource.open();

    // Return a list of tasks saved in the database
    List<Task> tasks = datasource.getAllTasks();

    // Use the SimpleCursorAdapter to show the elements in a ListView
    ArrayAdapter<Task> adapter = new ArrayAdapter<Task>(this.getActivity(), android.R.layout.simple_list_item_1, tasks);
    setListAdapter(adapter);

    Button b = (Button) v.findViewById(R.id.addTask);
    b.setOnClickListener(this);

    return v;
    }
}

frag_todolist.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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <EditText
                android:id="@+id/item1Date"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:text="DD/MM"
                android:textAppearance="?android:attr/textAppearanceSmall" />

            <EditText
                android:id="@+id/item1"
                android:layout_width="0dp"
                android:layout_weight="4"
                android:layout_height="match_parent"
                android:gravity="left"
                android:text="Enter Title" />

        </LinearLayout>

        <EditText
            android:id="@+id/item1Des"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:text="Enter Description" />

    </LinearLayout>

    <Button
        android:id="@+id/addTask"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="ADD TASK"
        android:onClick="onClick" />

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

    <TextView
        android:id="@android:id/empty"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="No tasks added yet." />

</LinearLayout>

1 个答案:

答案 0 :(得分:4)

getView()会返回onCreateView()中返回的视图,因此当您在getView()中调用onCreateView()时,该视图应为null。

尝试更改此行

View v = inflater.inflate(R.layout.frag_todolist, null);
ListView listView = (ListView) getView().findViewById(android.R.id.list);

View v = inflater.inflate(R.layout.frag_todolist, null);
ListView listView = (ListView) v.findViewById(android.R.id.list);