java.lang.NullPointerException错误与适配器如何解决?

时间:2016-03-09 16:45:13

标签: android

我收到致命异常/空指针异常 我不知道是什么问题以及为什么会发生这种错误

如果需要任何其他信息,请告诉我们。

logcat的

> 03-09 18:18:52.423 3765-3765/com.mosabalzouby.myapplication
> E/AndroidRuntime: FATAL EXCEPTION: main
>                                                                               java.lang.NullPointerException
>                                                                                   at
> com.mosabalzouby.myapplication.IndianMoviesAdapter.onBindViewHolder(IndianMoviesAdapter.java:38)
>                                                                                   at
> com.mosabalzouby.myapplication.IndianMoviesAdapter.onBindViewHolder(IndianMoviesAdapter.java:16)
>                                                                                   at
> android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5217)
>                                                                                   at
> android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5250)
>                                                                                   at
> android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4487)
>                                                                                   at
> android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4363)
>                                                                                   at
> android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961)
>                                                                                   at
> android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1370)
>                                                                                   at
> android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333)
>                                                                                   at
> android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:562)
>                                                                                   at
> android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2900)
>                                                                                   at
> android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3071)
>                                                                                   at android.view.View.layout(View.java:11180)
>                                                                                   at android.view.ViewGroup.layout(ViewGroup.java:4203)
>                                                                                   at android.widget.RelativeLayout.onLayout(RelativeLayout.java:925)
>                                                                                   at android.view.View.layout(View.java:11180)
>                                                                                   at android.view.ViewGroup.layout(ViewGroup.java:4203)
>                                                                                   at
> android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1037)
>                                                                                   at
> android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:747)
>                                                                                   at
> android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
>                                                                                   at
> android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1156)
>                                                                                   at
> android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:760)
>                                                                                   at android.view.View.layout(View.java:11180)
>                                                                                   at android.view.ViewGroup.layout(ViewGroup.java:4203)
>                                                                                   at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
>                                                                                   at android.view.View.layout(View.java:11180)
>                                                                                   at android.view.ViewGroup.layout(ViewGroup.java:4203)
>                                                                                   at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
>                                                                                   at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
>                                                                                   at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
>                                                                                   at android.view.View.layout(View.java:11180)
>                                                                                   at android.view.ViewGroup.layout(ViewGroup.java:4203)
>                                                                                   at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
>                                                                                   at android.view.View.layout(View.java:11180)
>                                                                                   at android.view.ViewGroup.layout(ViewGroup.java:4203)
>                                                                                   at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
>                                                                                   at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
>                                                                                   at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
>                                                                                   at android.view.View.layout(View.java:11180)
>                                                                                   at android.view.ViewGroup.layout(ViewGroup.java:4203)
>                                                                                   at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
>                                                                                   at android.view.View.layout(View.java:11180)
>                                                                                   at android.view.ViewGroup.layout(ViewGroup.java:4203)
>                                                                                   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1468)
>                                                                                   at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2418)
>                                                                                   at android.os.Handler.dispatchMessage(Handler.java:99)
>                                                                                   at android.os.Looper.loop(Looper.java:137)
>                                                                                   at android.app.ActivityThread.main(ActivityThread.java:4340)
>                                                                                   at java.lang.reflect.Method.invokeNative(Native Method)
>                                                                                   at java.lang.reflect.Method.invoke(Method.java:511)
>                                                                                   at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
>                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
>                                                                                   at dalvik.system.NativeStart.main(Native Method)

MainActivity:

  List<IndianMovies> movies=new ArrayList<>();

        int posters[]={R.mipmap.avengers,R.mipmap.avengers,R.mipmap.avengers,R.mipmap.avengers,R.mipmap.avengers,R.mipmap.avengers};
        String moviename[]={"aaaaa","bbbbbb","ccccc","vvvvvv","eeeeeee","wwwwww"};
        String movierate[]={"9.9","5.5","2.3","4.4","5.1","6.2"};
        String moviestory[]={"aaaaaaaaaaa","aaaaaaaaa","wwwwwwwwwww","eeeee","rrrrrrr","qqqqq"};


        for (int i=0;i<posters.length;i++){
            IndianMovies movie=
                    new IndianMovies(moviename[i],movierate[i],moviestory[i],posters[i]);
            movies.add(movie);


        }

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        IndianMoviesAdapter adapter = new IndianMoviesAdapter(movies);
        recyclerView.setAdapter(adapter);

我的适配器:

public class IndianMoviesAdapter extends RecyclerView.Adapter<IndianMoviesAdapter.IndianMovieHolder>{

    List<IndianMovies> moviesList;
    public IndianMoviesAdapter(List<IndianMovies> moviesList){
        this.moviesList=moviesList;


    }


    @Override
    public IndianMovieHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View row = LayoutInflater.from(parent.getContext()).inflate(R.layout.indian_movie_row,parent,false);
        IndianMovieHolder holder = new IndianMovieHolder(row);

        return holder;
    }

    @Override
    public void onBindViewHolder(IndianMovieHolder holder, int position) {
    IndianMovies movie =moviesList.get(position);
        holder.movietitle.setText(movie.moviename);
        holder.movierat.setText(movie.movierate);
        holder.moviesdesc.setText(movie.moviestory);
        holder.poster.setImageResource(movie.posterimage);
    }

    @Override
    public int getItemCount() {
        return moviesList.size();
    }

    class IndianMovieHolder extends RecyclerView.ViewHolder{
        TextView movietitle,movierat,moviesdesc;
        ImageView poster;


        public IndianMovieHolder(View itemView) {
            super(itemView);


            movietitle =(TextView) itemView.findViewById(R.id.movietitleTV);
            movietitle =(TextView) itemView.findViewById(R.id.movieratTV);
            moviesdesc =(TextView) itemView.findViewById(R.id.moviedescTV);
            poster =(ImageView) itemView.findViewById(R.id.movieposteIMG);


        }
    }

}

如何解决?

1 个答案:

答案 0 :(得分:0)

问题在你的模型中是indianMovies我无法添加评论,因为我的回购很低请与我分享你的模特