我知道这个问题已被多次询问,但这些问题都没有解决我的问题。堆栈跟踪看起来像这样。
02-09 19:39:20.364 18846-18846/com.michael1011.tweetcomposer E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.michael1011.tweetcomposer, PID: 18846
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.michael1011.tweetcomposer/com.michael1011.tweetcomposer.login}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5466)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.support.design.widget.Snackbar.setText(Snackbar.java:334)
at android.support.design.widget.Snackbar.make(Snackbar.java:210)
at com.michael1011.tweetcomposer.login.onCreate(login.java:43)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5466)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
适配器类看起来与此类似
package com.example.babu.moviemanager;
import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
Context context;
List<Movie> movieList;
public CustomAdapter(Context context, List<Movie> movieList) {
this.context = context;
this.movieList = movieList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.items,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Movie movie=movieList.get(position);
holder.title.setText(movie.getTitle());
holder.overview.setText(movie.getOverview());
Picasso.with(getcontext()).load(movie.getPosterPath()).into(holder.poster);
}
@Override
public int getItemCount() {
return movieList.size();
}
public Context getcontext() {
return context;
}
public class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.imageContainer)
ImageView poster;
@BindView(R.id.titleContainer)
TextView title;
@BindView(R.id.overviewContainer)
TextView overview;
@BindView(R.id.card)
CardView cardView;
public ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(itemView);
}
}
}
我尝试了堆叠溢出的所有解决方案,但这些似乎都不适用,我会感激任何帮助的人。如果你打算投票,请告诉我为什么?
答案 0 :(得分:0)
根据butterknife文档,您在视图持有者中使用的绑定方法必须接受视图目标bind(this)
或查看目标并查看源bind(this,view)
。在您的情况下,将绑定方法更改为bind(this,itemView)
,它应该可以工作。
答案 1 :(得分:0)
将ButterKnife依赖项更改为最新版本可以为我解决。
dependencies {
...
implementation 'com.jakewharton:butterknife:10.2.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.0'
}