itslequid
@itslequid
Android - разработка, 12 лет отроду

Что делать, если выбивает в NullPointerException, хотя всё вроде как правильно?

Привет всем!
Когда я захожу в скомпиленное приложение,оно вылетает,а в logcat выдается следующая ошибка:
Attempt to invoke virtual method 'void android.widget.TextView.setVisibility(int)' on a null object reference

Погуглив, понял, что скорее всего я просто ссылаюсь на нулевой объект, но,посмотрев в код, понял,что это не так.
Прошу помочь исправить ошибку.
Код MoviesAdapter.java:
Код
public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.MyViewHolder> {
    private List<Movie> moviesList;
    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView title, genre, number, available;

        public MyViewHolder(View view) {
            super(view);
            title = (TextView) view.findViewById(R.id.title);
            genre = (TextView) view.findViewById(R.id.genre);
            number = (TextView) view.findViewById(R.id.count);
            available = (TextView) view.findViewById(R.id.ddd);
        }
    }


    public MoviesAdapter(List<Movie> moviesList) {
        this.moviesList = moviesList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.movie_list_row, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Movie movie = moviesList.get(position);
        holder.title.setText(movie.getTitle());
        holder.genre.setText(movie.getGenre());
        holder.number.setText(movie.getDocumentId() + " урок");
        holder.available.setVisibility(View.VISIBLE);


    }

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


И XML код:
Код
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/selectableItemBackground"
    android:clickable="true"
    android:focusable="true"
    android:orientation="vertical"
    android:paddingBottom="@dimen/row_padding_vertical"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/row_padding_vertical"
    >

    <TextView
        android:id="@+id/count"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/title"
        android:fontFamily="@font/r_regular"
        android:textSize="10sp"
        android:letterSpacing=".15"
        android:textAllCaps="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="-46dp"
        android:text="1 УРОК" />

    <TextView
        android:id="@+id/genre"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/title"
        android:layout_marginStart="0dp"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="4dp"
        android:textColor="#B0B0B0"
        android:layout_toEndOf="@+id/location"
        android:layout_toRightOf="@+id/location"
        android:fontFamily="@font/r_regular"
        android:text="99 кабинет"
        android:letterSpacing=".04"
        android:textSize="12sp" />


    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="22dp"
        android:text="Заголовок"
        android:fontFamily="@font/r_regular"
        android:textColor="@color/title"
        android:textSize="24sp"
        />

    <View
        android:layout_width="5dp"
        android:layout_height="43dp"
        android:layout_alignEnd="@+id/title"
        android:layout_alignRight="@+id/title"
        android:layout_marginStart="10dp"
        android:layout_marginTop="4dp"
        android:layout_marginEnd="1dp"
        android:layout_marginRight="1dp"
        android:background="@drawable/rect"
        android:foregroundGravity="right"
        android:layout_marginLeft="10dp" />
    <View
        android:layout_width="match_parent"
        android:layout_height="2sp"

        android:layout_marginTop="120dp"
        android:background="@drawable/line" />

    <ImageView
        android:id="@+id/location"
        android:layout_width="15dp"
        android:layout_height="15dp"
        android:layout_below="@+id/title"
        android:layout_alignStart="@+id/count"
        android:layout_alignLeft="@+id/count"
        android:layout_marginStart="0dp"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="5dp"
        android:background="@drawable/location"
        android:maxWidth="15dp"
        android:maxHeight="15dp" />

    <TextView
        android:id="@+id/ddd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/genre"
        android:layout_marginStart="76dp"
        android:layout_marginLeft="76dp"
        android:layout_toEndOf="@+id/location"
        android:visibility="invisible"
        android:layout_toRightOf="@+id/location"
        android:text="TextView" />


</RelativeLayout>
  • Вопрос задан
  • 379 просмотров
Решения вопроса 1
itslequid
@itslequid Автор вопроса
Android - разработка, 12 лет отроду
Спасибо llerik за помощь.
Действительно, было два лэйаута: movie_list_row.xml и movie_list_row.xml(v21)
5bfd923b94685278082835.png
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы