Android Listview适配器在片段中给出空指针异常

时间:2014-02-04 12:26:20

标签: android file listview

这里使用的代码我从另一个非常好的片段类中拉出来。

我想从data/data/目录中提取文件列表,并将它们显示在ListView中。

  

抛出的错误位于第58行.lv.setAdapter。

我已经从Eclipse切换到Android工作室,所以也许这与它有关?

Logcat:

02-04 12:21:48.535    7433-7433/com.super8bit.singoffbeta E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.super8bit.singoffbeta.UserGamesHandler.SetListViewAdapter(UserGamesHandler.java:58)
            at com.super8bit.singoffbeta.UserGamesHandler.onCreateView(UserGamesHandler.java:33)

我的代码是:

public class UserGamesHandler extends android.support.v4.app.Fragment implements

android.view.View.OnClickListener {

File path = new File(Environment.getRootDirectory().getAbsolutePath().toString());

ArrayList<String> fileList = new ArrayList<String>();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_user_games, container,
            false);

    getVideos(path);
    SetListViewAdapter();

    return view;
}

@Override
public void onClick(View view) {

}

private void getVideos(File f){

    File[] files = f.listFiles();
    //fileList.clear();
    for (File file : files){
        fileList.add(file.getName().toString()+".MP4  ");
}
}

private void SetListViewAdapter() {
    ListView lv = (ListView) getActivity().findViewById(R.id.listViewTest);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
            R.layout.custom_listview, fileList);

    lv.setAdapter(adapter);

    Toast.makeText(getActivity().getApplicationContext(), fileList.toString(), Toast.LENGTH_LONG).show();
  }
}

2 个答案:

答案 0 :(得分:3)

声明您查看onCreateView以上的变量,并替换此行

ListView lv = (ListView) getActivity().findViewById(R.id.listViewTest);

ListView lv = (ListView) view.findViewById(R.id.listViewTest);

答案 1 :(得分:1)

 public class UserGamesHandler extends android.support.v4.app.Fragment implements

 android.view.View.OnClickListener {

 File path = new File(Environment.getRootDirectory().getAbsolutePath().toString());

 ArrayList<String> fileList = new ArrayList<String>();

@Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_user_games, container,
        false);

getVideos(path);
SetListViewAdapter();

return view;
}    
 @Override
public void onActivityCreated (Bundle savedInstanceState)
{
    super.onActivityCreated(savedInstanceState);
 getVideos(path);
SetListViewAdapter();

}


private void getVideos(File f){

File[] files = f.listFiles();
//fileList.clear();
for (File file : files){
    fileList.add(file.getName().toString()+".MP4  ");
 }
  }

private void SetListViewAdapter() {
ListView lv = (ListView) getView().findViewById(R.id.listViewTest);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
        R.layout.custom_listview, fileList);

lv.setAdapter(adapter);

Toast.makeText(getActivity().getApplicationContext(), fileList.toString(), Toast.LENGTH_LONG).show();
  }
}
   }