BaseFragment 的库项目中的 ButterKnife 视图绑定失败

ButterKnife view binding in Library project for BaseFragment fails

我正在创建一个库模块,我试图在其中使用 ButterKnife。在这个模块中,我有一个 BaseFragment class,所有片段都从中派生。这是 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 class 来绑定这样的视图 -

@BindView(R2.id.button_selection_done)
private ImageButton mSelectionDoneButton;

这是在派生的 Fragment class 中完成的。我设置了一个调试断点,它显示 none 我的视图即使在使用 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.access0(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.access0(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。

您缺少第二个必需的包括:

apt 'com.jakewharton:butterknife-compiler:8.4.0'

GitHub documentation page 上阅读更多相关信息。

在 Fragment 中尝试像这样在 onViewCreated 中进行绑定:

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    ButterKnife.bind(this, view);
}