0

我正在尝试获取列表视图内的文件夹中的文件名(该列表视图位于 ViewPager 的布局内),但我得到了 NullPointerException 并且我不知道出了什么问题。

 public void citire() {
    ListView lv1 = (ListView) findViewById(R.id.lv1);
    myList = new ArrayList<String>();

    File directory = Environment.getExternalStorageDirectory();
    file = new File(directory + "/InterSRL");
    File list[] = file.listFiles();

    for (int i = 0; i < list.length; i++) {
        myList.add(list[i].getName());
    }
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1, myList);
    lv1.setAdapter(adapter);
}

我在lv1.setAdapter(adapter);(第 169 行)收到错误消息

 06-28 13:10:57.987: E/AndroidRuntime(30450): Caused by: java.lang.NullPointerException
 06-28 13:10:57.987: E/AndroidRuntime(30450):   at        com.example.testlayout.Gestionare.citire(Gestionare.java:169)
 06-28 13:10:57.987: E/AndroidRuntime(30450):   at com.example.testlayout.Gestionare.onCreate(Gestionare.java:33)
 06-28 13:10:57.987: E/AndroidRuntime(30450):   at android.app.Activity.performCreate(Activity.java:5104)
 06-28 13:10:57.987: E/AndroidRuntime(30450):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
 06-28 13:10:57.987: E/AndroidRuntime(30450):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

编辑:这是我的 viewPager 的整个活动:

public class Gestionare extends Activity {

public List<String> myList;
public File file;
ViewPager vp;
LinearLayout gestionare1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gestionare);
    citire();
    ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);

    MyPagerAdapter adap = new MyPagerAdapter(this);
    vp.setAdapter(adap);

    vp.setOnPageChangeListener(new OnPageChangeListener() {

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageSelected(int arg0) {
            // TODO Auto-generated method stub

        }

    });

}

private class MyPagerAdapter extends PagerAdapter {
    private ArrayList<LinearLayout> arry;

    public MyPagerAdapter(Context context) {
        arry = new ArrayList<LinearLayout>();
        // arry.add(new ListView1Page(context));
        arry.add(new LinearLayout(context));
        // views.add(new ListView2Page(context));
        arry.add(new LinearLayout(context));
        // use this when design done graphically
    }

    @Override
    public void destroyItem(View view, int arg1, Object object) {
        ((ViewPager) view).removeView((LinearLayout) object);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return arry.size();
    }

    @Override
    public Object instantiateItem(View view, int position) {
        // View myView = views.get(position);
        // ((ViewPager) view).addView(myView);
        // return myView;
        //

        View v = new View(Gestionare.this.getApplicationContext());
        final LayoutInflater inflater = (LayoutInflater) Gestionare.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        switch (position) {
        case 0:
            v = inflater
                    .inflate(R.layout.activity_gestionare2, null, false);

            break;
        case 1:
            v = inflater
                    .inflate(R.layout.activity_gestionare1, null, false);

            break;
        default:

            break;
        }
        ((ViewPager) view).addView(v, 0);

        return v;
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {

        return arg0 == arg1;

    }

    public Parcelable saveState() {
        return null;
    }

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(intent);
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

public void citire() {

    myList = new ArrayList<String>();
    vp = (ViewPager) findViewById(R.id.pager);
    gestionare1 = (LinearLayout) findViewById(R.id.gestionare1);
    ListView lv1 = (ListView) gestionare1.findViewById(R.id.lv1);

    File directory = android.os.Environment.getExternalStorageDirectory();
    file = new File(directory.getAbsolutePath() + "/InterSRL");
    File list[] = file.listFiles();

    for (int i = 0; i < list.length; i++) {
        myList.add(list[i].getName());
    }
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1, myList);
    lv1.setAdapter(adapter);
}

}

4

2 回答 2

0

如果崩溃线是

lv1.setAdapter(adapter);

这意味着它lv1是空的,这意味着findViewById在指定的布局中没有找到ListView带有 id的R.id.lv1

于 2013-06-28T10:31:17.033 回答
0

例如,如果您在 ViewPager 中使用 FragmentStatePagerAdapter 来初始化列表,则需要执行以下操作:

    @Override
public Fragment getItem(int thePosition)
{
    return fragments.get(thePosition);
}

然后在你的 Fragment 中:

   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View layout = inflater.inflate(R.layout.your_layout_with_list, null);


ListView lv1 = (ListView) layout .findViewById(R.id.lv1);
myList = new ArrayList<String>();

File directory = Environment.getExternalStorageDirectory();
file = new File(directory + "/InterSRL");
File list[] = file.listFiles();

for (int i = 0; i < list.length; i++) {
    myList.add(list[i].getName());
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, android.R.id.text1, myList);
lv1.setAdapter(adapter);
    return layout;
}
于 2013-06-28T10:33:06.710 回答