使用ListFragment和ViewPager时出现“源未找到”的错误

3

我一直在尝试创建一个简单的横向滚动选项。

我有两个ListFragments,我希望在它们之间滚动。

当从onCreateView函数返回View时,我收到“源未找到”错误。

这是我的MainActivity类:

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {

    AppSectionsPagerAdapter mAppSectionsPagerAdapter;
    ViewPager mViewPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
        final ActionBar actionBar = getActionBar();
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mAppSectionsPagerAdapter);
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }
        });
        for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
            actionBar.addTab(
                    actionBar.newTab()
                            .setText(mAppSectionsPagerAdapter.getPageTitle(i))
                            .setTabListener(this));
        }
    }

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

    @Override
    public void onTabSelected(ActionBar.Tab tab, android.app.FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, switch to the corresponding page in the ViewPager.
        mViewPager.setCurrentItem(tab.getPosition());
    }

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

    public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {

        public AppSectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }
        Fragment a;
        @Override
        public Fragment getItem(int i) {
            switch (i) {
                case 0:
                    return new FragmentA();

                default:
                    return new FragmentB();
            }
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return "Section " + (position + 1);
        }
    }
}

这是我的ListFragment类之一(第二个类的名称和布局文件不同):

    public class FragmentA extends ListFragment {

        @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.favorites_layout, container, false);
                updateStationsView(0);
                return rootView;
            }
}

updateStationView是用于填充列表的函数。移除它并没有帮助,所以我认为它是无害的。

错误是在以下语句之后抛出的:

return rootView;

这段代码返回到MainActivity的onCreate函数。

当我将ListFragment更改为简单的Fragment时,它可以工作。我认为我缺乏一些知识来解决这个问题。我真的想使用ListFragment...

有人能帮忙吗? 非常感谢您的努力。

添加XML文件:

activity_main.xml:

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

favorites_layout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">

   <ListView 
     android:id="@+id/list"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent" />
</LinearLayout>

你能发布错误日志吗? - Philipp Jahoda
将您的ListView的id更改为“android:id = "@ android:id / list"” - Philipp Jahoda
1个回答

1
我很确定问题在于你的ListFragment自定义布局没有包含ListView。这些代码行会导致错误,因为你需要在你的"favourites_layout" .xml文件中放置一个ListView。
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      View rootView = inflater.inflate(R.layout.favorites_layout, container, false);
      updateStationsView(0);
      return rootView;
 }

ListFragment有一个默认布局,由单个列表视图组成。然而,如果你希望的话,可以通过从onCreateView(LayoutInflater, ViewGroup, Bundle)返回你自己的视图层次结构来自定义片段布局。为此,你的视图层次结构必须包含一个ListView对象,其id为"@android:id/list"(如果在代码中,则为"list")。因此,你的布局可能看起来像这样:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">

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

更多信息请参见: http://developer.android.com/reference/android/app/ListFragment.html


我已经将XML文件更改为包含一个LinearLayout和一个ListView元素。请参见主题中的新XML。但它没有起作用。 - gioravered
请阅读我的完整答案。您需要将列表视图的id设置为"@android:id/list"。 - Philipp Jahoda
抱歉,我漏掉了“Android”这部分。非常感谢 - 它正在工作! - gioravered

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接