0

I have a main class where I have a CardView and one add button. On clicking add Button, we can add a cardview below. How this works is basically that I have incorporated card.xml layout into my main class as a container layout that is added.

Now I want to assign each card a number (set), basically display 1, 2, 3 etc. that is increasing with each added card view below the main one on the top.

However, when I increase the number in the addcard method, the app crashes.

In main class:

    int set = 1;

in onCreate method:

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


    add = findViewById(R.id.addperff);
    layout = findViewById(R.id.container);


    textView2 = findViewById(R.id.textViewSets);
    textView2.setText(""+set);


    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
      
            addCard();


        }
    });
    Intent secondIntent = getIntent();    //getting the title from other class
    String message = secondIntent.getStringExtra("One");


    TextView textView = (TextView) findViewById(R.id.type);

    textView.setText(message);
}



    private void addCard() {
        final View view = getLayoutInflater().inflate(R.layout.card, null);
        ++set;
        TextView textView3 = findViewById(R.id.textViewSets2);
        textView3.setText(""+set);
        layout.addView(view);
    }
}

Basically I have a horizontal layout with text(views) on top and edittexts for the user put in a number. A horizontal layout with 3 cardviews basically.

  • what error do you get? Add it to your question. Also mention on which line it crashes – Sambhav Khandelwal Apr 05 '22 at 16:18
  • I get a Fatal Exception seemingly caused by the addCard method. When I press the add button the app closes basically. java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.example.myapp.app.MyActivity.addCard – mebeingaprogrammer Apr 05 '22 at 19:22
  • I think I have found the reason for this: I am not able to access the textview from the card.xml file here in my main class basically. When I put "setContentView(R.layout.card);" in the add method before the textview, I do get the cardview with the right number displayed. But of course, I only get a single card now (card number 2). Maybe I need to modify the line "final View view = getLayoutInflater().inflate(R.layout.card, null);" ? – mebeingaprogrammer Apr 05 '22 at 19:41
  • Can you update the question to include the stacktrace, along with full on create method? Also, what do you mean by `main class`? Are you inside an Activity or Fragment? – Naveed Apr 06 '22 at 00:59
  • @Naveed I'm inside an activity, yes. – mebeingaprogrammer Apr 06 '22 at 08:59
  • I have seen that people with similiar tasks work with an adapter class. For example here: https://stackoverflow.com/questions/59413480/how-to-display-numbers1-2-3-in-cardview-items But then again I don't have a fixed length since I'm adding cards manually. I would know how to go about it if it was a list of images, that I want to get the position of. – mebeingaprogrammer Apr 06 '22 at 09:10

2 Answers2

0

Why dont you try this?:

private void addCard() {
        final View view = getLayoutInflater().inflate(R.layout.card, null);
        ++set;
        TextView textView = new TextView(this);
        textView.setText(""+set);
        layout.addView(view);
    }
Sambhav Khandelwal
  • 3,585
  • 2
  • 7
  • 38
  • Thank you, but unfortunately not. It might have worked if I only had 1 textview in the card layout resource. But I actually have many. Basically I have a horizontal layout with text(views) on top and edittexts for the user put in a number. A horizontal layout with 3 cardviews basically. Thanks again and sorry if I'm not explaining it well. – mebeingaprogrammer Apr 06 '22 at 08:50
0

Try changing

    TextView textView3 = findViewById(R.id.textViewSets2);

to

    TextView textView3 = view.findViewById(R.id.textViewSets2);

I'm assuming this R.id.textViewSets2 is inside the just inflated view, and not inside your activity at that point

Ivo
  • 18,659
  • 2
  • 23
  • 35