0

I'm a little bit new using Butterknife and I'm having a problem. I want click on a textview and the open another activity. I know how to do that without using Butterknife, but this is what I did so far:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    ButterKnife.bind(this);


}

@OnClick(R.id.tv_forgot_pass)
void forgotPassClick(){
        Context mContext = LoginActivity.this;
        Class nextActivity = ForgotPasswordActivity.class;
        Intent mIntent = new Intent(mContext, nextActivity);
        startActivity(mIntent);
        finish();
    }
}

My problem right now is I don't know how to use my onClick method, because if I put the that method inside the onCreate, the activity will open the next activity immediately. I tried to use tv_forgot_pass.setOnClickListener and didn't work. And also doesn't work in the xml because the method doesnt have a View as parameter.

It is okey what Im doing? or there is another way to set a clicklistner with Butterknife?

I'm going to explain why is not duplicated with this question

First, they are using and old version of Butterknife, I'm using the 8.8.1 version and the "duplicate question" is using 6.1.0. My version doesnt support InjectView(it's Bind now). Second, my question talks about click on a TextView, the other question talks about click on a button. It's similar but not the equal. And the most important, I read the "duplicate question" like an hour ago, before I asked my question and because I didn't find a solution to my problem I decided to post my question.

LordCommanDev
  • 922
  • 12
  • 38

5 Answers5

1

For onClick

@OnClick(R.id.tv_forgot_pass)
    public void onForgotPassClick(View view) {
        Context mContext = LoginActivity.this;
        Intent mIntent = new Intent(mContext, ForgotPasswordActivity.class);
        startActivity(mIntent);
        finish();
    }
Jay Rathod
  • 11,131
  • 6
  • 34
  • 58
1

java.lang.IllegalStateException: Could not find method forgotPassClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatTextView with id 'tv_forgot_pass'

Logcat showing IllegalStateException

Signals that a method has been invoked at an illegal or inappropriate time. In other words, the Java environment or Java application is not in an appropriate state for the requested operation.

  • You should Use AppCompatTextView instead of TextView.
  • Remove android:onClick="forgotPassClick" from XML.

XML

<android.support.v7.widget.AppCompatTextView 
    android:id="@+id/tv_forgot_pass"  
    android:focusable="true" 
    .... />

Then

@OnClick(R.id.tv_forgot_pass)
void forgotPassClick(){
        Context mContext = LoginActivity.this;
        Intent mIntent = new Intent(mContext, ForgotPasswordActivity.class);
        startActivity(mIntent);
        finish();
    }
}
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
  • 1
    `android:onClick="forgotPassClick"` should be removed. he is using butterknife – Raghunandan Apr 06 '18 at 06:18
  • @Raghunandan sir `AppCompatTextView ` case ?? – IntelliJ Amiya Apr 06 '18 at 06:18
  • but if I deleted the onClick, nothing happens – LordCommanDev Apr 06 '18 at 06:20
  • And when I use onclick, the xml shows this message "Corresponding method handler void forgotPassClick() not found' – LordCommanDev Apr 06 '18 at 06:22
  • @Coeus Read https://stackoverflow.com/questions/9852792/android-sdk-tools-rev-17-onclick-corresponding-method-handler-not-found – IntelliJ Amiya Apr 06 '18 at 06:22
  • The guy is get the name of the method from the string values but I'm not. – LordCommanDev Apr 06 '18 at 06:27
  • 1
    I added this to the forgotPassClick(View view) and the xml, finally, recognized the method and also, finally,.. it works! Thanks man! – LordCommanDev Apr 06 '18 at 06:35
  • @Raghunandan Sir, Thanks a lot! – IntelliJ Amiya Apr 06 '18 at 06:37
  • 1
    I just realized that Jay Rathod RJ answered about an hour that I need to use a View as parameter. Lol. I didn´t notice it but I will accept this answer. Thanks for your time! – LordCommanDev Apr 06 '18 at 06:40
  • 2
    @Coeus you don't need that( android:onClick ="forgotPassClick") in your xml. You should remove that since you are using butterknife. it should work. as i said there is nothing wrong with your code. you should also post the relevant xml layout and check listener bindings @ http://jakewharton.github.io/butterknife/ for more info. Quoting **All arguments to the listener method are optional**. – Raghunandan Apr 06 '18 at 07:30
  • @Raghunandan yes sir. – IntelliJ Amiya Apr 06 '18 at 07:32
  • 1
    Ok, finally I know what was my mistake. I think we only have to use this implementation 'implementation 'com.jakewharton:butterknife:8.8.1' but it's necessary to use this one 'annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1''. Now works fine! – LordCommanDev Apr 06 '18 at 16:56
  • Yes yes.Sloppy mistake. You should read before write code. https://github.com/JakeWharton/butterknife – IntelliJ Amiya Apr 06 '18 at 17:00
0

Whatever you are doing is right.

@OnClick(R.id.tv_forgot_pass)
void forgotPassClick(){
        Context mContext = LoginActivity.this;
        Class nextActivity = ForgotPasswordActivity.class;
        Intent mIntent = new Intent(mContext, nextActivity);
        startActivity(mIntent);
        finish();
    }

above code will be executed when you will click on tv_forgot_pass.

Vir Rajpurohit
  • 1,869
  • 1
  • 10
  • 23
0

make some change. first bind text view as local of class like below ..

    @BindView(R.id.myTextView)
TextView myTextView;

then after onCreate method bind butter knife like below ..

        ButterKnife.bind(this);

then after apply click event like below ..

    @OnClick(R.id.myTextView)
private void goNextActivity(){
    Intent mIntent = new Intent(this, ForgotPasswordActivity.class);
    startActivity(mIntent);
    finish();
}

and both activity define in android manifest file.

0

Use @Bind before oncreate will work fine

public class Login extends AppCompatActivity {

@Bind(R.id.tv_forgot_pass)
    Button button;

   @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    ButterKnife.bind(this);


}

@OnClick(R.id.tv_forgot_pass)
public void forgotPassClick(){
        Context mContext = LoginActivity.this;
        Class nextActivity = ForgotPasswordActivity.class;
        Intent mIntent = new Intent(mContext, nextActivity);
        startActivity(mIntent);
        finish();
    }
}
Imanshu
  • 73
  • 11