[英]Android Listview adapter giving null pointer exception in fragment
The code used here I pulled out of another fragment class which works very well. 我在这里使用的代码退出了另一个效果很好的片段类。
I want to pull a list of files from the data/data/
directory and display them in a ListView. 我想从
data/data/
目录中提取文件列表,并在ListView中显示它们。
The error thrown is at line 58. lv.setAdapter.
引发的错误在第58行。lv.setAdapter。
I have switched to Android studio from Eclipse so maybe that has something to do with it? 我已经从Eclipse切换到Android Studio,所以这可能与它有关吗?
Logcat : 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)
My code is: 我的代码是:
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();
}
}
Declare you view variable above onCreateView
, and replace this line 声明您在
onCreateView
上方查看变量,并替换此行
ListView lv = (ListView) getActivity().findViewById(R.id.listViewTest);
with 同
ListView lv = (ListView) view.findViewById(R.id.listViewTest);
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();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.