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.xm
l 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.
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