我尝试在ViewPager中使用ListView,但无法创建适配器。它基本上崩溃了。我有以下代码(由eclipse的模板制成):

  public class WeeklyActivity extends FragmentActivity {

/**
 * The {@link android.support.v4.view.PagerAdapter} that will provide
 * fragments for each of the sections. We use a
 * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
 * will keep every loaded fragment in memory. If this becomes too memory
 * intensive, it may be best to switch to a
 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
 */
SectionsPagerAdapter mSectionsPagerAdapter;

/**
 * The {@link ViewPager} that will host the section contents.
 */
ViewPager mViewPager;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_weekly);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(
            getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);




}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.weekly, menu);
    return true;
}

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a DummySectionFragment (defined as a static inner class
        // below) with the page number as its lone argument.
        Fragment fragment = new DummySectionFragment();
        Bundle args = new Bundle();
        args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 5;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
        case 0:
            return "p1";
        case 1:
            return "p2";
        case 2:
            return "p3";
        case 3:
            return "p4";
        case 4:
            return "p5";
        }
        return null;
    }
}

/**
 * A dummy fragment representing a section of the app, but that simply
 * displays dummy text.
 */
public static class DummySectionFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";

    public DummySectionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = (RelativeLayout) inflater.inflate(R.layout.fragment_weekly_dummy, container, false);
        ListView lv = (ListView) rootView.findViewById(R.id.time_tv);
        String[] lessons = new String[] {"a", "a"};
        ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(rootView.getContext(), android.R.layout.simple_list_item_1, lessons);
        lv.setAdapter(listAdapter);
        return rootView;
    }
}

}


fragment_weekly_dummy:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".WeeklyActivity$DummySectionFragment" >

    <ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:id="@+id/weekly_list"
    android:padding="20dp" />

</RelativeLayout>


LogCat:

12-10 14:28:58.275: D/AndroidRuntime(20794): Shutting down VM
12-10 14:28:58.275: W/dalvikvm(20794): threadid=1: thread exiting with uncaught exception (group=0x41aa6700)
12-10 14:28:58.283: E/AndroidRuntime(20794): FATAL EXCEPTION: main
12-10 14:28:58.283: E/AndroidRuntime(20794): java.lang.NullPointerException
12-10 14:28:58.283: E/AndroidRuntime(20794):    at com.dcapps.lessons.WeeklyActivity$DummySectionFragment.onCreateView(WeeklyActivity.java:137)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at android.view.View.measure(View.java:15848)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at android.view.View.measure(View.java:15848)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at android.view.View.measure(View.java:15848)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at android.view.View.measure(View.java:15848)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at android.view.Choreographer.doFrame(Choreographer.java:532)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at android.os.Handler.handleCallback(Handler.java:730)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at android.os.Looper.loop(Looper.java:137)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at android.app.ActivityThread.main(ActivityThread.java:5103)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at java.lang.reflect.Method.invokeNative(Native Method)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at java.lang.reflect.Method.invoke(Method.java:525)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-10 14:28:58.283: E/AndroidRuntime(20794):    at dalvik.system.NativeStart.main(Native Method)

最佳答案

使用getActivity()

ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, lessons);


public final Activity getActivity ()

Return the Activity this fragment is currently associated with.


编辑

你有这个

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:id="@+id/weekly_list" // id is weekly_list
    android:padding="20dp" />


还有这个

ListView lv = (ListView) rootView.findViewById(R.id.time_tv); // id is not weekly_list


ID确实符合

应该

ListView lv = (ListView) rootView.findViewById(R.id.weekly_list);


还要确保将活动上下文用于arrayadapter。

10-08 17:55