fragment
: все элементы необходимо получить через view
View yourView = view.findViewById(R.id.text_first);
activity
: можно напрямую получить доступ к элементу
View yourView = findViewById(R.id.text_first);
Ваш случай fragment
, так что есть два способа сделать это
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_first, container, false);
View showCountTextView = view.findViewById(R.id.text_first);
Button button = view.findViewById(R.id.count_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
countMe(view, showCountTextView); // 1
countMe(view); // 2
}
});
return view;
}
- передать
showCountTextView
на countMe
веселье c
private void countMe(View view, View showCountTextView) {
String countString = showCountTextView.getText().toString();
Integer count = Integer.parseInt(countString);
count++;
showCountTextView.setText(count.toString());
}
или получить TextView
view
получить
showCountTextView
от
view
private void countMe(View view) {
View showCountTextView = view.findViewById(R.id.text_first);
String countString = showCountTextView.getText().toString();
Integer count = Integer.parseInt(countString);
count++;
showCountTextView.setText(count.toString());
}