0

I have a file called row_list.xml which holds the style of a CardView. This CardView is the root object of this xml file. I'm using this CardView in a RecyclerView in my activity_main.

row_list.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <LinearLayout
        //codes>

        <TextView
            //codes />

    </LinearLayout>

</androidx.cardview.widget.CardView>

In my MainActivity.java I want to get the current height of this CardView, and then use this height for something. But when I call the code below, I got Attempt to invoke virtual method 'int androidx.cardview.widget.CardView.getHeight()' on a null object reference.

MainActivity.java

public class MainActivity extends AppCompatActivity {

    CardView cardView;

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

    cardView = findViewById(R.id.card_view);

    System.out.println(cardView); //prints null

    int height = cardView.getHeight(); //I get "null" object error

Why does cardView = findViewById(R.id.card_view) returns null? How can I get the attributes of a CardView? Couldn't find a related question/solution on the web.

Edit after comment

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {layoutInflater = LayoutInflater.from(context);

        View v = layoutInflater.inflate(R.layout.row_list,parent,false);
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }
GLHF
  • 3,835
  • 10
  • 38
  • 83
  • `cardView = findViewById(R.id.card_view)` comes from `row_list.xml` and your activity layout is `activity_main.xml` – IntelliJ Amiya May 21 '21 at 08:31
  • `cardView = findViewById(R.id.card_view)` should be in Adapter section – IntelliJ Amiya May 21 '21 at 08:31
  • you need to declare and calculate the height of cardview in the related adapter class. – Tausif May 21 '21 at 08:35
  • @Tausif could you give an example how to do that? I can share the part of the adapter that is related to that .xml file – GLHF May 21 '21 at 08:37
  • Please share your adapter class – Tausif May 21 '21 at 08:37
  • @Tausif Here the part related to that .xml `@NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {layoutInflater = LayoutInflater.from(context); View v = layoutInflater.inflate(R.layout.row_list,parent,false); ViewHolder vh = new ViewHolder(v); return vh; }` – GLHF May 21 '21 at 08:39
  • Please add this code in Question – Tausif May 21 '21 at 08:40
  • @Tausif Just edited. – GLHF May 21 '21 at 08:43
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/232684/discussion-between-tausif-and-glhf). – Tausif May 21 '21 at 08:43

0 Answers0