我制作了一个自定义的ArrayAdapter,它接受一组Movie对象,然后将电影的海报图像放入图像视图,将标题放入文本视图。Movie类的字段如下所示;它有标准的getter和setter。
public class Movie {
protected String id;
protected String posterPath;
protected String title;
这是自定义的ArrayAdapter构造函数:
public class MovieAdapter extends ArrayAdapter<Movie> {
private static final String LOG_TAG = MovieAdapter.class.getSimpleName();
/**
*
* @param context Current context used to inflate layout file
* @param movies list of Movie objects that will be displayed in list
*/
public MovieAdapter(Activity context, List<Movie> movies){
super(context, 0, movies);
Log.d(LOG_TAG, "adapter constructor");
if(movies != null){
for(int i = 0; i < movies.size(); i++){
Log.d(LOG_TAG, movies.get(i).getTitle());
}
}
}
这里是调用适配器的OnCreateView代码片段:
public class MovieFragment extends Fragment {
//number of movies to be displayed
private static final int ARG_NUM_MOVIES = 40;
private static final String LOG_TAG = MovieFragment.class.getSimpleName();
private List<Movie> movieInfo;
private MovieAdapter mMovieAdapter;
public MovieFragment() {
// Required empty public constructor
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
Log.d(LOG_TAG, "onCreateView Start");
mMovieAdapter = new MovieAdapter(getActivity(), movieInfo);
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
Log.d(LOG_TAG, "listview creation");
ListView listView = (ListView) rootView.findViewById(R.id.listview_movies);
Log.d(LOG_TAG, "listview creation2");
listView.setAdapter(mMovieAdapter);
/*listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String
}
}*/
Log.d(LOG_TAG, "return statement");
return rootView;
}
根据日志,我推断将适配器设置为listView时出错,但我不知道是什么原因导致的。我还证明了自定义适配器中的列表以/null开头是空的(如果不是总是这样)。
以下是导致我的应用程序崩溃的错误日志:
06-02 15:47:47.276 22311-22311/com.example.android.popularmovies D/MovieFragment: onCreateView Start
06-02 15:47:47.276 22311-22311/com.example.android.popularmovies D/MovieAdapter: adapter constructor
06-02 15:47:47.286 22311-22311/com.example.android.popularmovies D/AbsListView: Get MotionRecognitionManager
06-02 15:47:47.286 22311-22311/com.example.android.popularmovies E/MotionRecognitionManager: mSContextService = android.hardware.scontext.ISContextService$Stub$Proxy@1816532
06-02 15:47:47.286 22311-22311/com.example.android.popularmovies E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@7998283
06-02 15:47:47.286 22311-22311/com.example.android.popularmovies E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@7998283
06-02 15:47:47.286 22311-22311/com.example.android.popularmovies D/MovieFragment: listview creation
06-02 15:47:47.286 22311-22311/com.example.android.popularmovies D/MovieFragment: listview creation2
06-02 15:47:47.286 22311-22311/com.example.android.popularmovies D/AndroidRuntime: Shutting down VM
06-02 15:47:47.286 22311-22311/com.example.android.popularmovies E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.popularmovies, PID: 22311
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.popularmovies/com.example.android.popularmovies.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:337)
at android.widget.ListView.setAdapter(ListView.java:508)
at com.example.android.popularmovies.MovieFragment.onCreateView(MovieFragment.java:54)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
movieInfo只是一个不指向任何对象的引用,这意味着它为null。
请创建movieInfo的对象,如:
movieInfo = new ArrayList<>();
并赋予它一些价值,比如:
movieInfo.add(new Movie("1","path one","ONE"));
movieInfo.add(new Movie("2","path two","TWO"));
movieInfo.add(new Movie("1","path three","THREE"));
movieInfo.add(new Movie("1","path four","FOUR"));
movieInfo.add(new Movie("1","path five","FIVE"));
然后将适配器设置为列表。