Android 找不到 changeBackground 方法

Android changeBackground method not found

Android Studio:所以我非常简单地尝试更改我的活动 activity "Stream Page Main" 的背景图片,这不是我的主要 activity,通过单击"Stream Page Main" activity 中的一个按钮。这应该是一行代码,但是,无论我尝试什么,我都会得到一个 Method not Found Exception。即使我尝试更改背景颜色,它也不起作用。

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

在所有这三个选项中,我得到了相同的异常。我在 xml 中的约束布局的 ID 是 constraintLayout 并且按钮 onclick 设置为触发 changeBackgroundOfStreamPage() 方法 - 那里没有错字,我复制了方法的名称。

代码中没有来自 android 工作室的评论。我认为它与针对 constraintLayout / activity 有关,但我不明白,为什么 "this." 不能解决问题...

这是个例外:

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.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.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) 

我假设您已通过视图的 onClick XML 属性将回调分配给 button4。任何按钮点击监听器的回调必须符合以下接口:

public static interface View.OnClickListener {

    abstract void onClick(View v);
}

因此,您必须在方法中包含参数 View v,如下所示:

public void changeBackgroundOfStreamPage(View v) {

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

}

这可以从您发布的错误中清楚地看出:

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'