3

I am experiencing some really wired things lately with Android Studio, In my Login fragment xml I have defined a Button and a TextView. Both the button and the textView are suppose to be clickable. However only the switchToSignUp method is resolvable in the xml file. when I try to assign onClick listenr for the Button I get a warning that says : Cannot resolve symbol "loginClick". The code compiles fine even if the warning exists however when I click the Button nothing happens. Am I missing something obvious or could it be a bug of the 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"
    ...
 />

here is how I have defined the onClick listeners :

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("");
}
cyc115
  • 635
  • 2
  • 14
  • 27
  • Have you tried setting the `OnClickListener` from your java class? – Carlos J Jul 25 '14 at 18:10
  • I would like to avoid using it because I find it rather clustering – cyc115 Jul 25 '14 at 18:11
  • Check [this](http://stackoverflow.com/questions/4153517/how-exactly-does-the-androidonclick-xml-attribute-differ-from-setonclicklistene) answer. If you are using `Fragment`s you might need to implement the listener from java – Carlos J Jul 25 '14 at 18:14
  • Thanks for the reply. I am using Fragment, however both of my onclick mthods are located in the Activity Class. The TextView can be clicked with no problem. – cyc115 Jul 25 '14 at 18:23

1 Answers1

4

As @CarlosJimenes has mentioned I have to assign the onClick Listener in the onCreateView () method of the LoginFragment class.

@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;
}
cyc115
  • 635
  • 2
  • 14
  • 27