1

I'm facing two issues

  • I have in Android Studio a Message that my Buttons are never used: buttonSand and buttonLehm

Here is my code

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.widget.TextView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public void buttonLehm(View view) {
    TextView tv = (TextView) getView().findViewById(R.id.lehmBodenTextView);
    if(tv.getVisibility() == View.INVISIBLE)
    {
        tv.setVisibility(View.VISIBLE);
    }
    else
    {
        tv.setVisibility(View.INVISIBLE);
    }
}

public void buttonSand(View view) {
    TextView tv = (TextView)getView().findViewById(R.id.sandBodenTextView);
    if(tv.getVisibility() == View.INVISIBLE)
    {
        tv.setVisibility(View.VISIBLE);
    }
    else
    {
        tv.setVisibility(View.INVISIBLE);
    }
}

and if I run my Code and get to this Fragment and want to test my Buttons, I get this Error Message:

AndroidRuntime: FATAL EXCEPTION: main java.lang.IllegalStateException: Could not find method buttonLehm(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'lehmBodenButton' at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:325) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284) at android.view.View.performClick(View.java:5198) at android.view.View$PerformClick.run(View.java:21147) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Here is the XML-file

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#2fb215"
android:columnOrderPreserved="true"
android:tag="BodenFragment"
android:id="@+id/bodenFragment">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/bodenArtenString"
    android:id="@+id/bodenSeiteUeberschrift"
    android:layout_row="0"
    android:layout_column="0"
    android:textSize="40dp"
    android:textAlignment="center" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/lehmBodenString"
    android:id="@+id/lehmBodenButton"
    android:layout_row="1"
    android:layout_column="0"
    android:onClick="buttonLehm"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/lehmBodenTextString"
    android:id="@+id/lehmBodenTextView"
    android:layout_row="2"
    android:layout_column="0"
    android:visibility="invisible" />

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/sandBodenString"
    android:id="@+id/sandBodenButton"
    android:layout_row="3"
    android:layout_column="0"
    android:onClick="buttonSand" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/sandBodenTextString"
    android:id="@+id/sandBodenTextView"
    android:layout_row="4"
    android:layout_column="0"
    android:visibility="invisible" />

I hope I put enough Code up so somebody can help me with my Problem. I do not know if this helps but somewhere I got the ErrorMessage that I have to import android.widget.View instead of android.view.View but I need both of those and I can not reproduce this error at the moment for clarification.

Thanks

Adije
  • 25
  • 5
  • Can you please provide the code of the fragment and the activity? – babadaba Jul 01 '16 at 10:38
  • What does your `onCreateView` method look like? – StuStirling Jul 01 '16 at 10:38
  • Is that the full code? You need to place your methods in a class. You can't have functions without a class in java. – miva2 Jul 01 '16 at 10:44
  • No that was not the full code, i provided minimal information, so that (I thought) the problem would be easier to find. Thanks for all your reply, I found the Problem myself. I had the functions in the fragment class and I had to put the functions of the fragment buttons in the activity. – Adije Jul 01 '16 at 13:07

3 Answers3

0

judging that it is only partial code and seeing that you do import android.support.v4.app.Fragment; my guess is that you put those two methods in a Fragment. The thing is that android:onClick checks for the method in the Activity. So what you just have to do is move those two methods from the Fragment to the Activity the Fragment's in. See also How to handle button clicks using the XML onClick within Fragments

Community
  • 1
  • 1
Ivo
  • 18,659
  • 2
  • 23
  • 35
  • Yes, thanks. I just found it myself before I had the opportunity to look up if my question was answered. That was exactly my problem. – Adije Jul 01 '16 at 13:08
0

It looks like your code for the buttons is in the Fragment. You need to change it to be in the Activity for the on-click methods to work.

Chris Deck
  • 146
  • 2
  • 9
-1
static TextView tv = (TextView) getView().findViewById(R.id.lehmBodenTextView);
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View v;
    //add button onclicklistener with:
    buttonLehm(v);
    buttonSand(v);
}
AbA2L
  • 110
  • 7