膨胀视图和对象Android

时间:2012-11-10 03:34:33

标签: java android android-viewpager layout-inflater

我有一个有两个视图的PagerAdapter。现在我通过将populateList()附加到按钮来从xml调用android:onClick="populateList()"。如果我尝试在主要活动中调用populateList(),则会在NullPointerException上创建ListView。如何让我的view/ListView充气,以便我可以在主要活动中使用它?

package com.itoxygen.publicsafety;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class Main extends Activity {
    /** Called when the activity is first created. */

    EditText alarmLabel;
    ArrayList<String> item = new ArrayList<String>();
    Dialog alarmDialog;
    ArrayAdapter<String> listAdapter;
    ListView list;
    private List<String> path = null;
    private String root;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);

        root = Environment.getExternalStorageDirectory().getPath(); //gets the root path of SD card

        MyPagerAdapter adapter = new MyPagerAdapter();
        ViewPager myPager = (ViewPager) findViewById(R.id.threepageviewer);
        myPager.setAdapter(adapter);
        myPager.setCurrentItem(0);
    }


    }

    public void populateList(View v) {      
        list.setAdapter(listAdapter);
    }

    ////////////////////////////////////////SWIPE NAVIGATION STUFF///////////////////////////////////////////////////////////////
    /**
     * 
     */
    private class MyPagerAdapter extends PagerAdapter {

        /**
         * Returns how many pages on the main Activity
         */
        public int getCount() {
            return 2; //increment this if adding pages
        }

        public Object instantiateItem(View collection, int position) {

            LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            /*
             * Add your layouts here
             */
            int resId = 0;
            switch (position) {
            case 0:
                resId = R.layout.activity_main_list;
                break;
            case 1:
                resId = R.layout.activity_main_tile;
                break;
            }

            View view = inflater.inflate(resId, null);
            ((ViewPager) collection).addView(view, 0);

            list = (ListView)findViewById(R.id.list);
            listAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, item);


            return view;
        }

        public void destroyItem(View arg0, int arg1, Object arg2) { ((ViewPager) arg0).removeView((View) arg2); }
        public void finishUpdate(View arg0) { }
        public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == ((View) arg1); }
        public void restoreState(Parcelable arg0, ClassLoader arg1) { }
        public Parcelable saveState() { return null; }
        public void startUpdate(View arg0) { }

    }

}

1 个答案:

答案 0 :(得分:0)

list.setAdapter(listAdapter);列表中,除非首先调用instantiateItem,否则列表将为空。将您的list作业转移到onCreate()