当我在Butterknife中使用BindView时,Geeting为null

时间:2017-09-27 05:52:58

标签: android butterknife

这里我的topActivity扩展了BaseActivity

activity_top.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="schemas.android.com/apk/res/android"; 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" android:orientation="vertical"> 
  <View android:id="@+id/activity_top_group_area1" 
   android:layout_width="wrap_content" 
   android:layout_height="match_parent"/>
 </LinearLayout> 

TopActivity.java

public class TopActivity extends BaseActivity {

    @BindView(R.id.activity_top_group_area1)

    View mViewClickArea1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_top);

        ButterKnife.bind(this);
        # do something... but mViewClickArea1 is null here.
     }
    }

BaseActivity扩展了AppCompactActivity,我在其中集中了权限工作

public class BaseActivity extends AppCompactActivity  {

# do some Run time Permission work that is ...
private int requestCodeM;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public void onRequestPermissionsResult(final int requestCode, @NonNull final String[] permissions, @NonNull int[] grantResults) {
    boolean isPermissionGranted = false;
    if (requestCodeM == requestCode) {
        if (grantResults.length > 0) {
            /*--------------------------------------------------*/
            for (int i = 0; i < grantResults.length; i++) {
               if(grantResults[i] == PackageManager.PERMISSION_GRANTED){
                   isPermissionGranted=true;
               }else {
                   isPermissionGranted=false;
                   break;
               }
            }
            permissiongrantedResult(requestCode,isPermissionGranted);
        }
    }
}

public void addRequestPermission(String[] permissionRequest, int requestCode) {
    this.requestCodeM = requestCode;
    ActivityCompat.requestPermissions(ActivityWithRequestPermission.this, permissionRequest, requestCode);
}

public abstract void permissiongrantedResult(int requestCode, boolean grantStatus);`enter code here`
}

请看这个。

1 个答案:

答案 0 :(得分:0)

在您的代码中尝试此操作。

将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="match_parent"
android:orientation="vertical">

<View
    android:id="@+id/activity_top_group_area1"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"/>
</LinearLayout>

检查project gradle

dependencies {
    classpath 'com.android.tools.build:gradle:2.3.3'
    // add this in your code
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'  
}

检查app gradle

apply plugin: 'com.android.application'
apply plugin: 'android-apt'  // add this 

...
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.jakewharton:butterknife:8.6.0'
    // add this 
    apt 'com.jakewharton:butterknife-compiler:8.6.0
}