1
這裏是我得到的GridView代碼:在片段添加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;
}
}
,這裏是我的MovieAdapter
類項目中的GridView:
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項目並且它正確地填充了)
這裏有什麼問題?