JakeWharton / butterknife

Bind Android views and callbacks to fields and methods.
http://jakewharton.github.io/butterknife/
Apache License 2.0
25.55k stars 4.6k forks source link

Butterknife in BaseActivty #836

Closed heyzhuyue closed 7 years ago

heyzhuyue commented 7 years ago

public class BaseActivity extends ActionBarActivity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); App.getInstance().mActivityStack.addActivity(this); initContentView(); ButterKnife.bind(this); attachUI(); initView(); initEvents(); } }

public class BaseRegisterActivity extends BaseActivity:

public abstract class BaseRegisterActivity extends BaseActivity{

@Bind(R.id.edit_user_phone)
protected EditText editUserPhone;

}

public class ShiMingRegisterActivity extends BaseRegisterActivity implements ShiMingRegisterContract.View {

@Bind(R.id.edit_user_idcard)
EditText editUserIdCard;
private ShiMingRegisterContract.Presenter mRegisterPresenter;

 private void commitRegister() {
    // TODO Auto-generated method stub
    String phoneNumber = editUserPhone.getText().toString();
}

}

when i click a button , log nullpointException in the method commitRegister(), editUserPhone is null

JakeWharton commented 7 years ago

There's not enough information here to figure out the problem. Can you provide a minimally reproducing sample?

heyzhuyue commented 7 years ago

include_bt.xml is a common xml ,so i want inject editQuery this view in the BaseMainActivity. when i click bt_test ,the editQuery is null in the MainActivity

d4l-w4r commented 7 years ago

Hey! I just tested the example code you gave here and it behaved as expected. When I typed something in the EditText and pushed the 'Test' button the text gets logged to the console (see screenshot below). I tested this on a Nexus 4 emulator running Android 5.1.1 and using ButterKnife version 8.4.0.

From your code I can see that you must be using an older version of ButterKnife (<= 7.0.1) because you are using the @Bind annotation where in newer versions you would have to use @BindView.

Since this is the only difference between the code you posted and the one I ran on my machine I suggest you try updating your version of ButterKnife and see if that resolves the issue.

EDIT I just tried your example with ButterKnife v. 7.0.0 and still can't reproduce your NPE. Maybe it would help nail down your problem if you posted a stacktrace of the error.

works for me

heyzhuyue commented 7 years ago

you get editQuery is not null from BaseMainActivity , the ButterKnife version is v7.0.0

why i get editQuery is null in the ButterKnife v7.0.1

d4l-w4r commented 7 years ago

Tried with 7.0.1 as well, still can't reproduce unfortunately. Do you have a stack trace? Or some more code from the 2 actual Activities that seem to cause the problem?

heyzhuyue commented 7 years ago

if do not have BaseMainActivity , this is useful

heyzhuyue commented 7 years ago

can you give me you test project? you can share you project in the github

heyzhuyue commented 7 years ago

i use butterknife v8.4.0 , this is useful . why not useful int the butterknife v7.0.1

d4l-w4r commented 7 years ago

I have posted a test repository here: https://github.com/danielw93/butterknife-issue-836 It uses the exact code you posted above, only alternating the butterknife versions.

There are 3 branches: master uses butterknife v.8.4.0, and a branch for 7.0.0 and 7.0.1 each. Again I can not reproduce your problem and I don't seem to completely understand your responses.

Just to clarify, could you answer the following points?

  1. version 8.4.0 works for you in your production code (yes/no)?
  2. version 8.4.0 works for you in my example code (yes/no)?
  3. version 7.0.1 works for you in your production code (yes/no)?
  4. version 7.0.1 works for you in my example code (yes/no)?
  5. version 7.0.0 works for you in your example code (yes/no)?
  6. version 7.0.0 works for you in my example code (yes/no)?

I still have the suspicion that your problem lies in your production code and can not be reliably reproduced by the minimal example you have posted here.

Again, maybe a stack trace from the error happening in your code or some more actual code would help. Is your code on GitHub?

heyzhuyue commented 7 years ago

thanks

d4l-w4r commented 7 years ago

@zy1301529626 sooo...was this helpful? Have you resolved the problem? Or is this still an issue for you? Otherwise @JakeWharton this can probably be closed

JakeWharton commented 7 years ago

Thanks!