Application ButterKnife.bind(this);

There is an activit, in it declared values ​​approximately as follows:

 @BindView(R.id.name) TextView name; @BindView(R.id.gender) TextView gender; 

In the onCreate method added:

 ButterKnife.bind(this); 

And below in my method I try to write as follows:

 private void someMethod() { name.setText(stringFormater.formatString(model.getTitle(), R.string.name)); gender.setText(stringFormater.formatString(model.getGender().toString(), R.string.score)); } 

But it turns out to start only as follows:

 private void someMethod() { name = (TextView) findViewById(R.id.name); name.setText(stringFormater.formatString(model.getTitle(), R.string.name)); gender= (TextView) findViewById(R.id.gender); gender.setText(stringFormater.formatString(model.getGender().toString(), R.string.score)); } 

But so the whole point is lost application

Full activation code:

 public class TopActivity extends AppCompatActivity { private Model model; @BindView(R.id.name) TextView name; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_top_story); model = getIntent().getParcelableExtra("key"); populateTextViews(); ButterKnife.bind(this); } private void populateTextViews() { StringFormater stringFormater = new StringFormater(this); name.setText(stringFormater.formatString(model.getTitle(), R.string.name)); } } 
  • The code you give should work. You either confused the ID or not rebuilt the project or called bind to setContentView or a bunch of options that you can think of until you reveal more what it means "does not work" and will not give error logs if it is - YuriiSPb
  • Show the whole activation code. - post_zeew

1 answer 1

You are trying to work with TextView before it is tied to ButterKnife 's ButterKnife , which is why you get an NPE .

Instead:

 populateTextViews(); ButterKnife.bind(this); 

should be:

 ButterKnife.bind(this); populateTextViews(); 

And it is better to register binding immediately after setContentView(...) :

 setContentView(R.layout.activity_top_story); ButterKnife.bind(this); 

And also, in build.gradle regarding ButterKnife should be two lines:

 compile 'com.jakewharton:butterknife:8.4.0' annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0' 

UPD . If you want to use ButterKnife in the adapter, then you need to ViewHolder in the ViewHolder constructor:

 public class MyViewHolder extends ViewHolder { @BindView(R.id.name) TextView mName; public MyViewHolder(View view) { ButterKnife.bind(this, view); } } 
  • tried to write and above, gives an error. Unable to start activity ComponentInfo {com.company.testapp / com.company.testapp.activi‌ ty.TopActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText (java.lang. CharSequence) 'on a null object reference - Inkognito
  • in this also, + points to lines of the code name.setText(stringFormater.formatString(model.getTitle(), R.string.name)); and populateTextViews(); - Inkognito
  • @Inkognito, Show build.gradle . - post_zeew
  • updated build.gradle, helped, used only compile 'com.jakewharton:butterknife:8.2.1' - Inkognito
  • one
    @Inkognito, Added in response. - post_zeew