Closed heyzhuyue closed 7 years ago
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.
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?
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!
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{
}
public class ShiMingRegisterActivity extends BaseRegisterActivity implements ShiMingRegisterContract.View {
}
when i click a button , log nullpointException in the method commitRegister(), editUserPhone is null