Attempt to invoke virtual method 'void android.widget.ImageView.setVisibility(int)' on a null object reference

Ibramazin :

I have 2 layout files (activity_test.xml) and (content.xml) and one java file (ActivityTest.java)

ActivityTest.java set its content layout from (activity_test.xml) as shown

ActivityTest.java

@Override
protected void onCreate(Bundle savedInstanceState) {
.....................

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);

}

activity_test.xml contains a recyclerView as shown

activity_test.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
tools:context=".ActivityTest">

............
some views
...........

<androidx.recyclerview.widget.RecyclerView
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:id="@+id/recycler_view2"
 android:layout_marginTop="30dp"
 android:layout_alignParentBottom="true"
 android:scrollbars="vertical"
 app:layout_constraintBottom_toBottomOf="parent"
 app:layout_constraintHorizontal_bias="0.0"
 app:layout_constraintLeft_toLeftOf="parent"
 app:layout_constraintRight_toRightOf="parent"
 app:layout_constraintTop_toTopOf="parent"
 app:layout_constraintVertical_bias="0.0"
            />

I have a RecyclerViewAdapter that inflates the (content.xml) layout as shown

RecyclerViewAdapter

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {

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

Here is the content.xml

content.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".TestActivity">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>

...........

 <ImageView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/save"
  android:src="@drawable/outline_share_black_48dp"
  android:layout_toLeftOf="@+id/savePhoto"
  android:layout_marginTop="5dp"
  android:layout_marginRight="20dp"
  android:clickable="true"
  android:onClick="saveIntent"
            />

The problem - Setting a click Listener on the ImageView above with the id(save) throws that error.

ClickEvents such as - OnClickListener, OnTouchLlistener, setVisibility all throws the error - "Attempt to invoke virtual method 'void android.widget.ImageView.setVisibility(int)' on a null object reference"

e.g

MainActivity.java

..........

private ImageView save_icon;

..........

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);

.............
save_icon = findViewById(R.id.save);
........

public void saveIntent(View view){
    if(view.getId() == R.id.savePhotoIcon){
         save_icon.setImageResource(R.drawable.outline_bookmark_black_48dp);
    }
 }

I want to change the image src of the ImageView to another Image onClick

I tried doing this without xml - (android:Clickable and android:OnClick) but still throwing thesame error.

Most likely the id(save) in content.xml is returning a null value because it wasnt't used as the setContentView layout in onCreate in MainActivity.Java file.

When I tried thesame thing on a view from (activity_test.xml), it works.

Although, Actions such as - Displaying a toast message, New Intent all works. e.g

 public void saveIntent(View view){
    Intent intent = new Intent(ActivityTest.this, AnotherActivity.class);
    startActivity(intent);
}
vishal desai :

This is because you're creating the view holders at run time using layout template, and you call the view holder before the view shows on screen. For this, you have to implement it in onBindViewHolder:

 public void onBindViewHolder(MyViewHolder holder, int position)
 {
     ImageView imgView=holder.itemview.findViewById(R.id.save);
     imgView.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                 // Code here executes when user click on save
             }
         });
 }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=301047&siteId=1