Android changeBackground method not found

77 views Asked by At

Android Studio: so I am very simply trying to change the background image of my active activity "Stream Page Main", which is not my main activity, by clicking on a button in this "Stream Page Main" activity. This should be one line of code, however, no matter what I try, I get a Method not Found Exception. Even if I try to change the background colour instead, it doesnt work.

public class StreamPageMain extends Activity {

public void changeBackgroundOfStreamPage(){

    StreamPageMain.this.findViewById(android.R.id.content).setBackgroundResource(R.drawable.streaming_background_grey);

    //this.findViewById(android.R.id.content).setBackgroundColor(Color.BLACK);

    //ConstraintLayout mConstraintLayout = (ConstraintLayout)findViewById(R.id.constraintLayout);
    //mConstraintLayout.setBackgroundResource(R.drawable.streaming_background_grey);
}
}

On all of these three options I get the same Exception. The ID of my constraint Layout in the xml is constraintLayout and the button onclick is set to trigger the changeBackgroundOfStreamPage() method - there is no typo in there, I copied the name of the method.

No remarks from android studio within the code. I assume it has something to do with targeting the constraintLayout / the activity, but I do not see, why "this." doesn't do the trick...

this is the exception:

05-24 11:28:03.646 5776-5776/comn.example.ezio.streamingapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: comn.example.ezio.streamingapp, PID: 5776
java.lang.IllegalStateException: Could not find a method changeBackgroundOfStreamPage(View) in the activity class comn.example.ezio.streamingapp.StreamPageMain for onClick handler on view class android.widget.Button with id 'button4'
at android.view.View$1.onClick(View.java:4015)
at android.view.View.performClick(View.java:4788)
at android.view.View$PerformClick.run(View.java:19896)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5258)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NoSuchMethodException: changeBackgroundOfStreamPage [class android.view.View]
at java.lang.Class.getMethod(Class.java:661)
at java.lang.Class.getMethod(Class.java:640)
at android.view.View$1.onClick(View.java:4008)
at android.view.View.performClick(View.java:4788) 
at android.view.View$PerformClick.run(View.java:19896) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5258) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
1

There are 1 answers

3
nstosic On BEST ANSWER

I'm assuming that you've assigned the callback to button4 via View's onClick XML attribute. The callback for any button click listener has to conform to the following interface:

public static interface View.OnClickListener {

    abstract void onClick(View v);
}

So, you have to include parameter View v in your method, like this:

public void changeBackgroundOfStreamPage(View v) {

    StreamPageMain.this.findViewById(android.R.id.content).setBackgroundResource(R.drawable.streaming_background_grey);

}

This can clearly be seen in the error that you've posted:

Could not find a method changeBackgroundOfStreamPage(View) in the activity class comn.example.ezio.streamingapp.StreamPageMain for onClick handler on view class android.widget.Button with id 'button4'