1

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();
}

}

Edward James
  • 121
  • 9

1 Answers1

2

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

Jason
  • 444
  • 4
  • 6
  • That's what I was thinking, but how can I do that with code?? – Edward James Jul 22 '20 at 16:46
  • I do have HomeFragment class which extends Fragment. Currently it has nothing on it. So if I want to do onClick in fragment class what must I do? Right now I just changed the tools:context in XML to .HomeFragment and did everything that I did in mainactivity in the fragment class and I am getting the same error. – Edward James Jul 22 '20 at 17:54
  • Ok I have done as presented and now I am getting Error: java.lang.IllegalStateException: Could not find method increaseNumber(View) in a parent or ancestor Context for android:onClick attribute defined on view class androidx.appcompat.widget.AppCompatImageButton with id 'focus_plus_button' I have also changed tools:context=".HomeFragment" in fragment XML file. Basically I don't think the Fragment class and Fragment XML file are being associated together. But I don't know how to fix. – Edward James Jul 22 '20 at 18:25
  • Updated Post with fragment class – Edward James Jul 22 '20 at 19:25
  • 2
    Remove the tools:context from xml – Jason Jul 22 '20 at 20:16
  • I am getting the same error as before: java.lang.IllegalStateException: Could not find method increaseNumber(View) in a parent or ancestor Context for android:onClick attribute defined on view class androidx.appcompat.widget.AppCompatImageButton with id 'focus_plus_button'. How would you add in buttons inside fragment xml and do the background work related to it in the fragment class? Just wondering because I feel like I am doing something wrong. – Edward James Jul 22 '20 at 20:21
  • 2
    Check here https://stackoverflow.com/a/6271637/8714139 – Jason Jul 22 '20 at 20:47
  • Oh yeah, thanks for the help my friend!!! I used the above method!!! – Edward James Jul 22 '20 at 22:58