Android Studio 按钮 onClick : Cannot resolve symbol

标签 android mobile android-studio

我最近在使用 Android Studio 时遇到了一些非常有线的事情,在我的登录 fragment xml 中,我定义了一个 Button 和一个 TextView。按钮和 textView 都应该是可点击的。但是,在 xml 文件中只有 switchToSignUp 方法是可解析的。当我尝试为按钮分配 onClick 监听器时,我收到一条警告:Cannot resolve symbol "loginClick"。即使存在警告,代码也能正常编译,但是当我单击按钮时,什么也没有发生。我是否遗漏了一些明显的东西,或者它可能是 Android Studio 的错误?

<Button
    android:text="login"
    android:onClick="loginClick"
    android:id="@+id/loginBtn"
    style="@android:style/Widget.Holo.Button.Borderless"
    android:background="@android:color/holo_red_light"
...
/>
<TextView
    android:text="No account ?"
    android:id="@+id/singin_signup_tv"
    android:clickable="true"
    android:onClick="switchToSignUp"
    ...
 />

这是我定义 onClick 监听器的方式:

public void switchToSignUp(View view){
    Log.i(TAG,"switch to sign up ");
    switchView();
}

public void loginClick(View view){
    Log.i(TAG,"login clicked");
    new AsyncTask<String , Void , String>(){
        protected String doInBackground(String... params) {/*...*/  }
    }.execute("");
}

最佳答案

正如@CarlosJimenes 提到的,我必须在 LoginFragment 类的 onCreateView () 方法中分配 onClick 监听器。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container , Bundle savedInstanceState){
    inputView =  inflater.inflate(R.layout.fragment_login,
            container,
            false
    );
    Button btn = (Button) inputView.findViewById(R.id.loginBtn);
    btn.setOnClickListener( loginClicked );
    return inputView;
}

关于Android Studio 按钮 onClick : Cannot resolve symbol,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24961903/

相关文章:

java - 通过网络发送数据时触发 Android 事件。检测网络使用情况

android - 移动设备上的按钮动态填充/高度问题

android - 移动设备的固定网页宽度

jquery - Fancybox 2(移动端)如何在背景滚动的情况下保持固定位置?

android - 如何在 ConstraintLayout 中旋转 TextView 及其约束边界

java - "Zoom In/out "图像在 Android 中单击按钮?

android - Gradle:Android支持版本冲突

android - 错误 :Execution failed for task ':app:processDebugGoogleServices' - com. google.android.gms

java - 错误 :(61, 48) 错误:不兼容的类型:字符串无法转换为 mpandroidchart 中的 List<BarEntry>

android - APP_ABI 忽略