Butterknife: Butterknife in BaseActivty

Created on 30 Dec 2016  路  12Comments  路  Source: JakeWharton/butterknife

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

Needs Info

Most helpful comment

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

All 12 comments

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

  • BaseActivity

    public abstract class BaseActivity extends ActionBarActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(getLayout());
    ButterKnife.bind(this);
    }
    
    
    public  abstract int getLayout();
    }
    
  • BaseMainActivity

    public abstract class BaseMainActivity extends BaseActivity {
    
    @Bind(R.id.edit_query)
    protected EditText editQuery;
    
    }
    
  • MainActivity

    public class MainActivity extends BaseMainActivity {
    
    private static final String TAG = "MainActivity";
    
    @Override
    public int getLayout() {
    // TODO Auto-generated method stub
    return R.layout.activity_main;
    }
    
    @OnClick(R.id.bt_test)
    void onBtclick(){
    Log.i(TAG, "editQuery="+editQuery.getText().toString());
    }   
    }
    
  • include_bt.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    
    
    <EditText
    android:id="@+id/edit_query"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
    
    
    </LinearLayout>
    
  • activity_main

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.example.butterknifer.MainActivity" >
    
        <include layout="@layout/include_bt"/>
    
        <Button
            android:id="@+id/bt_test"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="test" />
    
    </LinearLayout>
    

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

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

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

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?

if do not have BaseMainActivity , this is useful

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

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

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?

thanks

@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

Thanks!

Was this page helpful?
0 / 5 - 0 ratings