Reputation: 922
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.
Upvotes: 0
Views: 1839
Reputation: 75788
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.
AppCompatTextView
instead of
TextView
.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();
}
}
Upvotes: 1
Reputation: 83
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();
}
}
Upvotes: 0
Reputation:
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.
Upvotes: 0
Reputation: 11245
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();
}
Upvotes: 1
Reputation: 1937
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.
Upvotes: 0