The method setOnClickListener(View.OnClickListener) in the type View is not applicable

本文探讨了在Android开发中,从一个Activity启动另一个Activity时遇到的问题。具体分析了OnClickListener实现方式不当导致的错误,并给出了正确的解决方案。
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.view.View;



public class Activity_01Activity extends Activity {

    /** Called when the activity is first created. */
    private Button myButton = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        myButton=(Button)findViewById(R.id.MyButton);
        myButton.setText("second activity");
        myButton.setOnClickListener(new MyButtonListener());

    }

/*

   class MyButtonListener implements OnClickListener{
        @Override
             public void onClick(DialogInterface dialog, int which) {         //把鼠标移动到MyButtonListener上时,提示,点击添加,默认创建一个函数就是这个
                // TODO Auto-generated method stub
                Intent intent=new Intent();
                intent.setClass(Activity_01Activity.this,otheractivity.class);
                Activity_01Activity.this.startActivity(intent);
                }
        }

*/

    class MyButtonListener implements android.view.View.OnClickListener{
        @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent=new Intent();
                intent.setClass(Activity_01Activity.this,otheractivity.class);
                Activity_01Activity.this.startActivity(intent);
                }
        }

}


/* */注释部分替换掉后边一个class MyButtonListener implements android.view.View.OnClickListener{} 时,

出现下面错误, myButton.setOnClickListener(new MyButtonListener());

句子中setOnClickListener被红线显示,鼠标移动到上边时,提示下面信息:

The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (Activity_01Activity.MyButtonListener)


若如上所写,注释掉错误表述,运行无错误。

ps刚入门,看别人的没看到自己想要的回答,自己来一下,哈哈



/* * Copyright (C) 2018 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package androidx.databinding; import java.lang.annotation.ElementType; import java.lang.annotation.Target; /** * BindingAdapter is applied to methods that are used to manipulate how values with expressions * are set to views. The simplest example is to have a public static method that takes the view * and the value to set: * <p><pre> *<code>@BindingAdapter("android:bufferType") * public static void setBufferType(TextView view, TextView.BufferType bufferType) { * view.setText(view.getText(), bufferType); * }</code></pre> * In the above example, when android:bufferType is used on a TextView, the method * setBufferType is called. * <p> * It is also possible to take previously set values, if the old values are listed first: * <p><pre> *<code>@BindingAdapter("android:onLayoutChange") * public static void setOnLayoutChangeListener(View view, View.OnLayoutChangeListener oldValue, * View.OnLayoutChangeListener newValue) { * if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { * if (oldValue != null) { * view.removeOnLayoutChangeListener(oldValue); * } * if (newValue != null) { * view.addOnLayoutChangeListener(newValue); * } * } * }</code></pre> * When a binding adapter may also take multiple attributes, it will only be called when all * attributes associated with the binding adapter have binding expressions associated with them. * This is useful when there are unusual interactions between attributes. For example: * <p><pre> *<code>@BindingAdapter({"android:onClick", "android:clickable"}) * public static void setOnClick(View view, View.OnClickListener clickListener, * boolean clickable) { * view.setOnClickListener(clickListener); * view.setClickable(clickable); * }</code></pre> * The order of the parameters must match the order of the attributes in values in the * BindingAdapter. * <p> * A binding adapter may optionally take a class extending DataBindingComponent as the first * parameter as well. If it does, it will be passed the value passed in during binding, either * directly in the inflate method or indirectly, using the value from * {@link DataBindingUtil#getDefaultComponent()}. * <p> * If a binding adapter is an instance method, the generated DataBindingComponent will have * a getter to retrieve an instance of the BindingAdapter's class to use to call the method. */ @Target(ElementType.METHOD) public @interface BindingAdapter { /** * @return The attributes associated with this binding adapter. */ String[] value(); /** * Whether every attribute must be assigned a binding expression or if some * can be absent. When this is false, the BindingAdapter will be called * when at least one associated attribute has a binding expression. The attributes * for which there was no binding expression (even a normal XML value) will * cause the associated parameter receive the Java default value. Care must be * taken to ensure that a default value is not confused with a valid XML value. * * @return whether or not every attribute must be assigned a binding expression. The default * value is true. */ boolean requireAll() default true; } 翻译并注释
最新发布
09-25
layout.xml如下 <?xml version="1.0" encoding="utf-8"?> <!-- Copyright (C) 2017 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:paddingBottom="6dp"> <!-- <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Wakelock Anomaly" android:textSize="16sp"/> --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginLeft="50dp" android:layout_marginRight="50dp" > <Button android:id="@+id/hh_wifiScanning_button" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginRight="40dp" android:layout_weight="1" android:text="Scanning" android:onClick="HHwifiScanning"/> <Button android:id="@+id/hh_addwifi_button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="Add network" android:onClick="HHaddwifi"/> </LinearLayout> </LinearLayout> 那么点击事件应该如何写,具体一点
08-21
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值