Butterknife 和 TextView

Butterknife and TextViews

我以前从未使用过butterknife。我不想在我的项目中使用重复的 findViewById(...),而是想使用该库。但是我得到一个例外。

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                                            at popularmovies.theo.tziomakas.popularmovies.DetailsFragment.onResponse(DetailsFragment.java:224)

这是我的class。

public class DetailsFragment extends Fragment implements
                LoaderManager.LoaderCallbacks<Object>,
                TrailersAdapter.TrailersAdapterOnClickHandler{
            private static final int TRAILERS_LOADER_ID = 0;
            private static final int REVIEWS_LOADER_ID =   1;

            private static final String TRAILERS_LAYOUT = "DetailsFragment.trailer.layout";
            private static final String REVIEWS_LAYOUT = "DetailsFragment.review.layout";

            int recyclerViewOrientation = LinearLayoutManager.VERTICAL;

            private static final String TRAILERS_LIST = "trailers_list";
            private static final String REVIEWS_LIST = "reviews_list";
            /*****************************************************
             * Lists where fetched trailers and reviews are stored.
             *****************************************************/
            private List<Trailers> trailersList = new ArrayList<>();
            private List<Reviews> reviewsList = new ArrayList<>();

            private static final String[] FAVOURITE_MOVIE_PROJECTION = {
                    FavouriteContract.FavouriteEntry.COLUMN_MOVIE_ID,
                    FavouriteContract.FavouriteEntry.COLUMN_MOVIE_POSTER_PATH,
                    FavouriteContract.FavouriteEntry.COLUMN_MOVIE_TITLE,
                    FavouriteContract.FavouriteEntry.COLUMN_MOVIE_OVERVIEW,
                    FavouriteContract.FavouriteEntry.COLUMN_MOVIE_VOTE_AVERAGE,
                    FavouriteContract.FavouriteEntry.COLUMN_MOVIE_RELEASE_DATE
            };

            /**************************************************************************
             * Those custom adapter will actually send the views to the recycler views.
             **************************************************************************/
            private  TrailersAdapter trailersAdapter;
            private  ReviewsAdapter reviewsAdapter;

            /*******************************************
             * RecyclerViews that will display our data.
             *******************************************/
            private  RecyclerView trailersRecyclerView;
            private  RecyclerView reviewsRecyclerView;


            private static final String MOVIE_SHARE_HASHTAG = " #PopularMoviesApp";
            private static final String LOG_TAG = "DetailsFragment";

            private Cursor favoriteCursor;
            private String mMovieId;
            private String mImage;
            private String mTitle;
            private double mRating;
            private String mDate;
            private String mOverview;
            private String oldReviewText;

            @BindView(R.id.no_reviews_text_view)
            public TextView noReviewTextView;

            @BindView(R.id.no_trailers_text_view)
            public TextView noTrailersTextView;

            @BindView(R.id.movie_image)
            public ImageView i;

            @BindView(R.id.movie_title)
            public TextView t;

            @BindView(R.id.movie_rating)
            public TextView r;

            @BindView(R.id.movie_date)
            public TextView d;

            @BindView(R.id.movie_overview)
            public TextView o;
            ...
            ButterKnife.bind(getActivity());
             Call<Movies> call = apiService.getMovieDetails(mMovieId, BuildConfig.MOVIESDB_API_KEY);
    call.enqueue(new Callback<Movies>() {
        @Override
        public void onResponse(Call<Movies> call, Response<Movies> response) {
            Movies movie = response.body();
            mImage = movie.getImageThumbnail();
            mTitle = movie.getTitle();
            mOverview = movie.getOverview();
            mRating = movie.getRating();
            mDate = movie.getDate();

            Picasso.with(getContext()).load(NetworkUtils.IMAGE_URL + NetworkUtils.IMAGE_SIZE_185 + mImage).into(i);
            //I get the error here. 
            t.setText(mTitle);
            o.setText(mOverview);
            r.setText(String.valueOf(mRating));
            d.setText(mDate);

        }

        @Override
        public void onFailure(Call<Movies> call, Throwable t) {
            // Log error here since request failed
            Log.e(LOG_TAG, t.toString());
        }
    });

有什么想法吗?

谢谢,

西奥

您需要在方法内部调用ButterKnife.bind() ,不能在方法外部调用。对于片段,您需要在 onCreateView():

中调用它
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  View view = inflater.inflate(R.layout.fancy_fragment, container, false);
  ButterKnife.bind(this, view);
  // TODO Use fields...
  return view;
}

请阅读 http://jakewharton.github.io/butterknife/

中的文档