Geeting null when i use BindView in Butterknife

人走茶凉 提交于 2019-12-09 07:13:36

问题


Here my topActivity which extend 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.
     }
    }

And the BaseActivity Which extend AppCompactActivity in which i do some centralize permission work

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`
}

Kindly look at this.


回答1:


Try this in your code .

Change xml code to this

<?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>

Check 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'  
}

Check 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
}


来源:https://stackoverflow.com/questions/46440401/geeting-null-when-i-use-bindview-in-butterknife

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!