0

我想要做的是使用片段填充 Viewpager 内的 listView 但由于主类扩展FragmentActivityFragment不是ListActivity或者我应该使用 ListFragment 吗?在 LogCat 中指向这个mViewPager.setAdapter

public class tabs extends FragmentActivity implements ActionBar.TabListener {

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide fragments for each of the
     * three primary sections of the app. 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}.
     */
    AppSectionsPagerAdapter mAppSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will display the three primary sections of the app, one at a
     * time.
     */
    ViewPager mViewPager;

    @SuppressLint("NewApi")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.load_main_groups_activty);
        db = new DatabaseHandler(this);
        mContext = getBaseContext();

        //Need this for not getting null pointer exception 
        productsList = new  ArrayList<HashMap<String, ?>>();

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

        // Set up the action bar.
        final ActionBar actionBar = getActionBar();

        // Specify that the Home/Up button should not be enabled, since there is no hierarchical
        // parent.
        actionBar.setHomeButtonEnabled(false);
        //actionBar.setDisplayShowTitleEnabled(false);

        // Specify that we will be displaying tabs in the action bar.
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Set up the ViewPager, attaching the adapter and setting up a listener for when the
        // user swipes between sections.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mAppSectionsPagerAdapter); //Logacat Error: at line 127
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                // When swiping between different app sections, select the corresponding tab.
                // We can also use ActionBar.Tab#select() to do this if we have a reference to the
                // Tab.
                actionBar.setSelectedNavigationItem(position);
            }
        });

        // For each of the sections in the app, add a tab to the action bar.
        for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
            // Create a tab with text corresponding to the page title defined by the adapter.
            // Also specify this Activity object, which implements the TabListener interface, as the
            // listener for when this tab is selected.
            actionBar.addTab(
                    actionBar.newTab()
                            .setText(mAppSectionsPagerAdapter.getPageTitle(i))
                            .setTabListener(this));
        }
    }

    /**
     * A fragment that launches other parts of the demo application.
     */
public  static class LaunchpadSectionFragment extends ListFragment{



        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_section_launchpad, container, false);

            Holder holder = new Holder();

         View rootView = inflater.inflate(R.layout.load_main_groups_activty, container, false);

        LoadAllProducts task = new LoadAllProducts();
        task.execute();

        MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(
                mContext, productsList,
                R.layout.load_main_groups_listview, FolderName);

        holder.lv= (ListView)rootView.findViewById(R.id.listView);

        holder.lv.setAdapter(adapter);
            return rootView;
        }

 class Holder {
            ListView lv;
            ViewPager pager;
        }


        class LoadAllProducts extends AsyncTask<String, String, String> {


            /**
             * Before starting background thread Show Progress Dialog
             * */
            @Override
            protected void onPreExecute() {
                super.onPreExecute();

                pDialog = new ProgressDialog(getActivity());
                pDialog.setMessage("Loading products. Please wait...");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(false);

                pDialog.show();

            }

            /**
             * getting All products from url
             * */
            protected String doInBackground(String... args) {
                // Building Parameters


                return null;
            }

            /**
             * After completing background task Dismiss the progress dialog
             * **/
            protected void onPostExecute(String file_url) {
                // dismiss the dialog after getting all products
                pDialog.dismiss();
                // updating UI from Background Thread


                getActivity().runOnUiThread(new Runnable() {
                    public void run() {
                        /**
                         * Updating parsed JSON data into ListView
                         * */


                        MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(
                                mContext, productsList,
                                R.layout.load_main_groups_listview, FolderName);
                        setListAdapter(adapter);

                    }
                });

            }

        }
    }

load_main_groups_activty并且有另一个布局来自定义主布局上的视图load_main_groups_listview

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"  
                android:background="@color/white"     
                >
    <!-- Main ListView
         Always give id value as list(@android:id/list)
    -->
    <ListView
            android:id="@+id/listView"
            android:layout_width="fill_parent"
            android:layout_alignParentTop="true"
            android:layout_alignParentBottom="true"
            android:layout_height="wrap_content" 

            >
    </ListView>

</RelativeLayout>

日志猫:

07-18 10:27:34.366: E/AndroidRuntime(13948): FATAL EXCEPTION: main
07-18 10:27:34.366: E/AndroidRuntime(13948): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jinisys.restoplusordering/com.jinisys.restoplusordering.tabs}: java.lang.NullPointerException
07-18 10:27:34.366: E/AndroidRuntime(13948):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
07-18 10:27:34.366: E/AndroidRuntime(13948):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
07-18 10:27:34.366: E/AndroidRuntime(13948):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
07-18 10:27:34.366: E/AndroidRuntime(13948):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
07-18 10:27:34.366: E/AndroidRuntime(13948):    at android.os.Looper.loop(Looper.java:137)
07-18 10:27:34.366: E/AndroidRuntime(13948):    at android.app.ActivityThread.main(ActivityThread.java:4745)
07-18 10:27:34.366: E/AndroidRuntime(13948):    at java.lang.reflect.Method.invokeNative(Native Method)
07-18 10:27:34.366: E/AndroidRuntime(13948):    at java.lang.reflect.Method.invoke(Method.java:511)
07-18 10:27:34.366: E/AndroidRuntime(13948):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-18 10:27:34.366: E/AndroidRuntime(13948):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-18 10:27:34.366: E/AndroidRuntime(13948):    at dalvik.system.NativeStart.main(Native Method)
07-18 10:27:34.366: E/AndroidRuntime(13948): Caused by: java.lang.NullPointerException
07-18 10:27:34.366: E/AndroidRuntime(13948):    at com.jinisys.restoplusordering.tabs.onCreate(tabs.java:121)
07-18 10:27:34.366: E/AndroidRuntime(13948):    at android.app.Activity.performCreate(Activity.java:5008)
07-18 10:27:34.366: E/AndroidRuntime(13948):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
07-18 10:27:34.366: E/AndroidRuntime(13948):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
07-18 10:27:34.366: E/AndroidRuntime(13948):    ... 11 more
4

1 回答 1

0

长而短的答案是永远不要使用 ListActivity 和 ListFragment 或其任何变体。

只需使用常规的片段和活动,

  1. 将 ListView 放入您的布局中。
  2. 创建一个扩展 ArrayListAdapter(或 BaseAdapter 或其他)的私有类适配器。
  3. 在片段的 onActivityCreated() 上为其分配一个适配器

ListActivity 和 ListFragment 是如此有限,并试图隐藏和耦合一些既不应该隐藏也不应该耦合的东西。

Vogella 有一个很棒的教程

http://www.vogella.com/tutorials/AndroidListView/article.html

与片段中的用法没有什么不同。

于 2013-07-18T03:02:19.743 回答