0

2 つのビューを持つ PagerAdapter があります。現在、ボタンに a をpopulateList()付けて xml から呼び出しています。Main アクティビティ内で fromandroid:onClick="populateList()"を呼び出そうとすると、自分の に が作成されます。Main アクティビティ内で使用できるようにするにはどうすればいいですか?populateList()NullPointerExceptionListViewview/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) { }

    }

}
4

1 に答える 1

0

が最初に呼び出されない限り、list.setAdapter(listAdapter);リスト内は null になります。instantiateItemの割り当てを に移動listしますonCreate()

于 2012-11-10T05:07:02.157 に答える