以下是获取GridView的代码:
public class MoviesFragment extends Fragment {
AppController instance = AppController.getInstance();
List<Movie> movies = new ArrayList<Movie>();
String html;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_movies, container, false);
GridView gridView;
gridView = (GridView) rootView.findViewById(R.layout.fragment_movies);
movies = getMoviesList();
gridView.setAdapter(new MoviesAdapter(getActivity(), movies));
return rootView;
}
public List<Movie> getMoviesList()
{
List<Movie> movies = new ArrayList<Movie>();
try {
org.jsoup.nodes.Document doc = Jsoup.parse(getActivity().getAssets().open("site.html"), "UTF-8", "");
Elements get = doc.select("div.post.lit > h2");
for (Element element : get)
{
movies.add(new Movie(element.text()));
}
} catch (IOException e) {
// TODO Auto-generated catch block
}
System.out.println(movies.size());
return movies;
}
}
这里是gridView中项目的MovieAdapter
类:
public class MoviesAdapter extends ArrayAdapter<Movie>
{
List<Movie> moviesList;
Context context;
public MoviesAdapter(Context context, List<Movie> objects)
{
super(context, android.R.id.content, objects);
this.moviesList = objects;
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.fragment_movies_individual, parent);
TextView name = (TextView) view.findViewById(R.id.textView1);
TextView description = (TextView) view.findViewById(R.id.textView2);
name.setText(moviesList.get(position).getName());
description.setText(moviesList.get(position).getDescription());
return view;
}
}
当我运行应用程序时,它只是继续崩溃并将NullPointerException
放入gridView的设置适配器行中。(gridView.setAdapter(new MoviesAdapter(getActivity(), movies));
我检查了movieList项目,并且它已正确填充。
这里有什么问题?
答案 0 :(得分:1)
错误在于:
gridView = (GridView) rootView.findViewById(R.layout.fragment_movies);
您需要提供网格ID作为findViewById(int)
参数。现在你提供了布局的ID,这显然是错误的。