Search code examples
androidandroid-studioandroid-fragmentsandroid-activityonclick

Changing TextView Attributes from button onClick not working


So I have a xml fragment called fragment_home. Which has 2 buttons and a textview inside cardview. One of the button (tries to) change the value in the textview. On both the buttons I have an onClick. The tools:context for the fragment is set to .MainActivity. I also have a extend Fragment class which is associated with the xml fragment.

Whenever I try to call anything to change the TextView attributes such as setText,setTextSize I get an exception: java.lang.IllegalStateException: Could not execute method for android:onClick

If I do not have setText, setTextSize, the onClick works.

MainActivity:

public class MainActivity extends AppCompatActivity {

private int textViewData;
private TextView inputDataTextView;

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

    inputDataTextView= (TextView)findViewById(R.id.focus_counter);
}

//onClick For One of the buttons
    public void increaseNumber(View view) {
    textViewData++;
    // Crashes When I have This. Doesn't crash when I comment this.
    inputDataTextView.setTextSize(60);
    // Crashes When I have this. Doesn't crash when I comment this.
    inputDataTextView.setText(String.valueOf(textViewData);

    Toast.makeText(this, "TextView Value = " + textViewData, Toast.LENGTH_SHORT).show();
}

I feel like It's something to do with my fragment and activity. But I just do not know what the problem is. As soon as I call anything related to the textview in the onClick the app crashes. And the textview in the xml is set to 0 initially and when I increase the number, it increases, so thats working. Thanks for the help!!

HomeFragment:

public class HomeFragment extends Fragment {

private int textViewData;
private static final String TAG = "MyActivity";

private TextView inputDataTextView;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_home, container, false);

    inputDataTextView = view.findViewById(R.id.focus_counter);

    return view;
}

public void increaseNumber(View view) {
    textViewData++;
    //inputDataTextView.setText("t");
    inputDataTextView.setText(String.valueOf(inputViewData));
    //Toast.makeText(getContext(), "Focus Vale = " + inputViewData, Toast.LENGTH_SHORT).show();
}

}


Solution

  • Your inputDataTextView is null because its in fragment but your instantiating it from MainActivity, which is wrong. Move both the onClick methods and view to the fragment java code.

    Edit copy theses codes from MainActivity to HomeFragment before onCreateView:

    private int textViewData;
    private TextView inputDataTextView;
    

    Then these inside onCreateView before the return statement:

     inputDataTextView= 
       (TextView)findViewById(R.id.focus_counter);
    

    Then these method inside your fragment:

     public void increaseNumber(View view) {
     textViewData++;
    inputDataTextView.setTextSize(60);
    inputDataTextView.setText(String.valueOf(textViewData);
    Toast.makeText(this, "TextView Value = " + textViewData, Toast.LENGTH_SHORT).show();
    

    }

    It should work now