适配器中的空对象引用

时间:2019-07-17 07:23:49

标签: android

以下是代码错误:

  

尝试在空对象引用上调用虚拟方法TextView。

Youtube缩略图视图的错误相同。我不知道是什么引起了问题。我的xml具有相同的ID。我正在使用Firebase Realtime数据库检索值。这是我的FirebaseAdapter类:

这是我的LogCat:

 Process: com.example.android.youtubevideoplayer, PID: 13504
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.example.android.youtubevideoplayer.FirebaseAdapter.onBindViewHolder(FirebaseAdapter.java:45)
        at com.example.android.youtubevideoplayer.FirebaseAdapter.onBindViewHolder(FirebaseAdapter.java:20)
        at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
        at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
public class FirebaseAdapter extends RecyclerView.Adapter<FirebaseAdapter.ViewHolder> {

    Context context;
    ArrayList<YoutubeVideoModel> list;

    public FirebaseAdapter(Context context, ArrayList<YoutubeVideoModel> list){
        this.context=context;
        this.list=list;

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view= LayoutInflater.from( parent.getContext() ).inflate( R.layout.lectures_video,parent,false);
        ViewHolder viewHolder=new ViewHolder(  view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        YoutubeVideoModel youtubeVideoModel= new YoutubeVideoModel();
        youtubeVideoModel=list.get( position );
       holder.Title.setText( youtubeVideoModel.getTitle() );
        YoutubeVideoModel finalYoutubeVideoModel = youtubeVideoModel;
        holder.Image.initialize( Constants.DEVELOPER_KEY, new YouTubeThumbnailView.OnInitializedListener(  ){

            @Override
            public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader youTubeThumbnailLoader) {
                youTubeThumbnailLoader.setVideo( finalYoutubeVideoModel.getVideoId() );

                youTubeThumbnailLoader.setOnThumbnailLoadedListener( new YouTubeThumbnailLoader.OnThumbnailLoadedListener() {
                    @Override
                    public void onThumbnailLoaded(YouTubeThumbnailView youTubeThumbnailView, String s) {
                        youTubeThumbnailLoader.release();
                    }

                    @Override
                    public void onThumbnailError(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader.ErrorReason errorReason) {

                        Log.e(( FirebaseAdapter.class.getSimpleName() ),"Youtube thumbnail error");
                    }
                } );
            }

            @Override
            public void onInitializationFailure(YouTubeThumbnailView youTubeThumbnailView, YouTubeInitializationResult youTubeInitializationResult) {

                Log.e( ( FirebaseAdapter.class.getSimpleName() ),"Youtube initialization failed" );
            }
        } );

    }

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

 }

这是我的ViewHolder类

//This is class ViewHolder
    class ViewHolder extends RecyclerView.ViewHolder{

        public TextView Title;
        public YouTubeThumbnailView Image;

        public ViewHolder(@NonNull View itemView) {
            super( itemView );

            Title=(TextView)itemView.findViewById( R.id.video_title_label );
            Image=(YouTubeThumbnailView) itemView.findViewById(`enter code here` R.id.video_thumbnail_image_view );
        }
    }

这些是项目的build.gradle(应用程序:模块)依赖项:

    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    implementation 'com.google.firebase:firebase-core:17.0.0'
    implementation 'com.google.firebase:firebase-database:17.0.0'
    implementation 'com.android.support:design:28.0.0'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
    implementation 'androidx.recyclerview:recyclerview:1.0.0-beta01'
    implementation 'androidx.cardview:cardview:1.0.0-beta01'

    ////

    // Image loading lib
    implementation 'com.github.bumptech.glide:glide:4.9.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'

    // bind view
    implementation 'com.jakewharton:butterknife:10.1.0'
    annotationProcessor "com.jakewharton:butterknife-compiler:10.1.0"

}
apply plugin: 'com.google.gms.google-services'

请帮助我使用此代码。

1 个答案:

答案 0 :(得分:0)

尝试一下。

public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
...
((ViewHolde) holder).Title.setText( youtubeVideoModel.getTitle() );// 
...