我正在尝试将文件名放在listview中的文件夹中(listview在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);
}
}
答案 0 :(得分:0)
如果崩溃线是
lv1.setAdapter(adapter);
这意味着lv1
为空,这意味着findViewById
在指定的布局中未找到ListView
的{{1}}
答案 1 :(得分:0)
例如,如果您在ViewPager中使用FragmentStatePagerAdapter进行初始化列表,则需要执行以下操作:
@Override
public Fragment getItem(int thePosition)
{
return fragments.get(thePosition);
}
然后在你的片段中:
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;
}