listview.setAdapter(myAdapter)不使用自定义适配器代码

时间:2016-06-03 03:13:18

标签: java android listview android-arrayadapter

我制作了一个自定义的ArrayAdapter,它接受一组Movie对象,然后将影片的海报图像放入imageview,将标题放入textview。电影课的领域在这里可见;它有标准的吸气剂和固定剂。

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)

1 个答案:

答案 0 :(得分:1)

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"));

然后将适配器设置为列表。