Adije Adije - 5 months ago 13
Android Question

Button "MyButton" is never used

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
Process: artur_konrad.hochschule_trier.de.gartenhelfer, PID: 3387
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"
tools:context="artur_konrad.hochschule_trier.de.gartenhelfer.BodenSeite"
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

Answer

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