我正在创建一个库模块,我正在尝试使用ButterKnife。在这个模块中,我有一个BaseFragment类,从中派生出所有片段。这是BaseFragment的结构 -
public abstract class BaseFragment extends Fragment {
private View mRootView;
public BaseFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mRootView = inflater.inflate(getLayout(), container, false);
ButterKnife.bind(this, mRootView);
onSetupView(mRootView);
return mRootView;
}
public View getRootView() {
return mRootView;
}
protected abstract void onSetupView(View rootView);
protected abstract int getLayout();
}
由于它是一个库项目,我使用R2
而不是R
类来绑定视图,例如 -
@BindView(R2.id.button_selection_done)
private ImageButton mSelectionDoneButton;
这是在派生的Fragment类中完成的。我已经设置了一个调试断点,它显示即使在使用BindView注释后我的所有视图都没有被初始化。 ID是正确的,但它仍然无法正常工作。如果我使用findViewById()
,一切正常。以下是我使用gradle安装ButterKnife的方法 -
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
模块级build.gradle
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jakewharton.butterknife'
group = 'com.github.Abhi347'
android {
compileSdkVersion 24
buildToolsVersion "24.0.1"
defaultConfig {
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.0'
compile 'com.android.support:support-v4:24.2.0'
compile 'com.android.support:recyclerview-v7:24.2.0'
compile 'com.balysv:material-ripple:1.0.2'
compile 'com.jakewharton:butterknife:8.4.0'
testCompile 'junit:junit:4.12'
}
崩溃日志
--------- beginning of crash
10-11 19:47:21.082 19426-19426/noob.com.noobfilechooser E/AndroidRuntime: FATAL EXCEPTION: main
Process: noob.com.noobfilechooser, PID: 19426
java.lang.RuntimeException: Unable to start activity ComponentInfo{noob.com.noobfilechooser/com.noob.noobfilechooser.NoobFileActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setVisibility(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
at android.app.ActivityThread.access$900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5451)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setVisibility(int)' on a null object reference
at com.noob.noobfilechooser.fragments.NoobFileFragment.turnOnMultiSelectMode(NoobFileFragment.java:177)
at com.noob.noobfilechooser.fragments.NoobFileFragment.initializeRecyclerView(NoobFileFragment.java:84)
at com.noob.noobfilechooser.fragments.NoobFileFragment.onSetupView(NoobFileFragment.java:70)
at com.noob.noobfilechooser.fragments.BaseFragment.onCreateView(BaseFragment.java:24)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2080)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:604)
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1238)
at android.app.Activity.performStart(Activity.java:6340)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2397)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
at android.app.ActivityThread.access$900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5451)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
我有什么办法可以在图书馆模块中进行装订吗?
PS:如果需要检查,github上可以获得该库的稳定版本 - NoobFileChooser,但我在上传的源代码中没有使用ButterKnife。
答案 0 :(得分:3)
答案 1 :(得分:0)
在Fragment中尝试在onViewCreated中进行绑定,如下所示:
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
ButterKnife.bind(this, view);
}