【问题标题】:Android changeBackground method not found找不到Android changeBackground方法
【发布时间】:2018-05-24 18:09:29
【问题描述】:

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

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 studio 的评论。我认为它与定位约束布局/活动有关,但我不明白为什么是“这个”。不成功...

这是个例外:

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) 

【问题讨论】:

    标签: java android android-activity methodnotfound


    【解决方案1】:

    我假设您已通过 View 的 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'
    

    【讨论】:

    • 谢谢,那是我错过的!现在它不再崩溃并运行该方法,但背景保持不变 - 任何想法为什么?
    • 这很可能是因为您传递了 findViewById 方法的 id。尝试将其更改为findViewById(R.id.constraintLayout),因为您已经提到这是您在 XML 中的布局的 id。基本上,您需要传递要更改其背景的容器的 id。
    • 再次感谢您!我的最终代码是 public void changeBackgroundOfStreamPage(View v){ this.findViewById(R.id.constraintLayout).setBackgroundResource(R.drawable.streaming_background_grey); }
    猜你喜欢
    • 2020-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-05
    • 2023-04-05
    相关资源
    最近更新 更多