yoop****
10
2021-09-15 16:51:18
4
119

안드로이드 textview 오류 (NullPointerException) 해결좀요 ㅠㅠㅠ


 다른 클래스에서도 계속 recyclerview 사용했었고 잘 실행됐었는데 이 클래스에서만 textview가 널값이 뜨네요,, 해결가능하신분 계실까요? 실행시킬 때는 오류 없었습니다.

혹시 몰라 전체코드도 붙입니다..


<my_free_board_list_two.xml>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/my_free_board_recylcerview"/>



</LinearLayout>
item_article_two.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal">

<ImageView
android:id="@+id/writer_profile"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="10dp" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/writer_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="글쓴이" />

<TextView
android:id="@+id/writer_title"
android:text="제목"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/writer_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="작성시간"
android:layout_marginTop="10dp"/>

</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000000" />
</LinearLayout>

My_Free_Board_List_two.class
public class My_Free_Board_List_two extends AppCompatActivity {

private RecyclerView recyclerView;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_free_board_list_two);

recyclerView = (RecyclerView) findViewById(R.id.my_free_board_recylcerview);


getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

recyclerView.setLayoutManager(new LinearLayoutManager(My_Free_Board_List_two.this));
recyclerView.setAdapter(new My_Free_Board_List_two.RecyclerViewAdapter());


}

class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

List<Article_Model> articles;

public RecyclerViewAdapter() {

articles = new ArrayList<>();

FirebaseDatabase.getInstance().getReference().child("Free_Board").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) {
for(DataSnapshot item : snapshot.getChildren()){
articles.add(item.getValue(Article_Model.class));
}
notifyDataSetChanged();

recyclerView.scrollToPosition(articles.size() -1 );
}

@Override
public void onCancelled(@NonNull @NotNull DatabaseError error) {

}
});

}


@NonNull
@NotNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_article_two,parent,false);

return new BoardViewHolder(view);

}

@Override
public void onBindViewHolder(@NonNull @NotNull RecyclerView.ViewHolder holder, int position) {

BoardViewHolder boardViewHolder = ((BoardViewHolder)holder);

System.out.println(articles.get(position).nickname);

Log.d("park", articles.get(position).nickname);

boardViewHolder.writer_name.setText(articles.get(position).nickname);

boardViewHolder.writer_title.setText(articles.get(position).title);
boardViewHolder.writer_time.setText(articles.get(position).writing_time);

Glide.with(holder.itemView.getContext())
.load(articles.get(position).imageUri)
.apply(new RequestOptions().circleCrop())
.into(boardViewHolder.writer_profile);



}

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

private class BoardViewHolder extends RecyclerView.ViewHolder {

public TextView writer_name;
public TextView writer_title;
public TextView writer_time;
public ImageView writer_profile;


public BoardViewHolder(View view) {
super(view);

writer_name = (TextView)findViewById(R.id.writer_name);
writer_title = (TextView)findViewById(R.id.writer_title);
writer_time = (TextView)findViewById(R.id.writer_time);
writer_profile = (ImageView)findViewById(R.id.writer_profile);
}
}
}
}
0
  • 답변 4

  • UnknownDontknow
    801
    2021-09-15 18:20:22

    뷰홀더 생성자에서 view인자가 사용된곳이 없네요.

    넘어온 view에서 findViewById 호출해보세요

  • yoop****
    10
    2021-09-15 19:40:46

    oncreateviewholder 에서의 view 말씀이신가요?

  • UnknownDontknow
    801
    2021-09-15 20:01:38

    Boardviewholdet클래스에 텍스트뷰들  아이디 매칭시켜주는부분이요.

    writer_time = (TextView)view.findViewById(R.id.writer_time);
  • yoop****
    10
    2021-09-15 21:29:06

    해결됐습니다. 감사합니다!

  • 로그인을 하시면 답변을 등록할 수 있습니다.