Android使用ButterKnife,butterknife.bind(this)问题

时间:2018-12-20 13:17:22

标签: android binding butterknife

我正在运行自己的项目,并且正在使用ButterKnife。

我刚刚创建了一个自定义的EditText,当我运行调试以查看其外观时,我的应用程序无法启动,并且错误表明存在问题

ButterKnife.bind(this);

这是我的代码

 <?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.constraint.ConstraintLayout
    android:id="@+id/loginCl1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toTopOf="@+id/loginCl2"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_weight="3">

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="@id/loginCl1"
        app:layout_constraintStart_toStartOf="@+id/loginCl1"
        app:layout_constraintEnd_toEndOf="@+id/loginCl1"
        app:layout_constraintBottom_toTopOf="@+id/id"
        app:layout_constraintVertical_weight="4"
        android:src="@drawable/logo" />

    <com.fapa.brent.fapa.Common.BrentEditText
        android:id="@+id/id"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:hint="@string/enterid"
        app:layout_constraintBottom_toTopOf="@+id/pw"
        app:layout_constraintEnd_toEndOf="@+id/loginCl1"
        app:layout_constraintStart_toStartOf="@id/loginCl1"
        app:layout_constraintTop_toBottomOf="@+id/image"
        app:layout_constraintVertical_weight="1"/>

    <com.fapa.brent.fapa.Common.BrentEditText
        android:id="@+id/pw"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:hint="@string/enterpw"
        android:inputType="textPassword"
        app:layout_constraintBottom_toBottomOf="@id/loginCl1"
        app:layout_constraintEnd_toEndOf="@+id/loginCl1"
        app:layout_constraintStart_toStartOf="@id/loginCl1"
        app:layout_constraintTop_toBottomOf="@+id/id"
        app:layout_constraintVertical_weight="1"/>
</android.support.constraint.ConstraintLayout>

<android.support.constraint.ConstraintLayout
    android:id="@+id/loginCl2"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/loginCl1"
    app:layout_constraintVertical_weight="2">

    <CheckBox
        android:id="@+id/saveId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/saveId"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <Button
        android:id="@+id/login"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/login"
        app:layout_constraintTop_toBottomOf="@+id/saveId"
        app:layout_constraintEnd_toStartOf="@id/signup"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/signup"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/signup"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/saveId"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintStart_toEndOf="@id/login" />

    <Button
        android:id="@+id/findId"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/findid"
        app:layout_constraintEnd_toStartOf="@+id/findPw"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/login" />

    <Button
        android:id="@+id/findPw"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/findpw"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/findId"
        app:layout_constraintTop_toBottomOf="@+id/signup" />

</android.support.constraint.ConstraintLayout>

和我的MainActivity.java

public class MainActivity extends AppCompatActivity {

@BindView(R.id.id)
EditText mId;
@BindView(R.id.pw)
EditText mPw;
@BindView(R.id.login)
Button mBtnLogin;
@BindView(R.id.signup)
Button mBtnSignup;
@BindView(R.id.findId)
Button mBtnFindId;
@BindView(R.id.findPw)
Button mBtnFindPw;


@OnClick(R.id.login)
void loginValidatation() {
    String id = mId.getText().toString();
    String pw = Sha512.getSHA512(mPw.getText().toString());

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);
    Stetho.initializeWithDefaults(this);

}

}

这是我自定义的EditText代码

public class BrentEditText extends android.support.v7.widget.AppCompatEditText implements TextWatcher, View.OnTouchListener, View.OnFocusChangeListener {

private Drawable clearDrawable;
private OnFocusChangeListener onFocusChangeListener;
private OnTouchListener onTouchListener;

public BrentEditText(Context context) {
    super(context);
    init();
}


public BrentEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public BrentEditText(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}

@Override
public void setOnFocusChangeListener(OnFocusChangeListener onFocusChangeListener) {
    this.onFocusChangeListener = onFocusChangeListener;
}

@Override
public void setOnTouchListener(OnTouchListener onTouchListener) {
    this.onTouchListener = onTouchListener;
}

//add 'X' button as well as Listeners
private void init() {
    Drawable clear = ContextCompat.getDrawable(getContext(), android.support.v7.appcompat.R.drawable.abc_ic_clear_material);
    clearDrawable = DrawableCompat.wrap(clear);
    DrawableCompat.setTintList(clearDrawable, getHintTextColors());
    clearDrawable.setBounds(0,0,clearDrawable.getIntrinsicWidth(), clearDrawable.getIntrinsicHeight());

    setClearIconVisible(false);

    super.setOnFocusChangeListener(this);
    super.setOnTouchListener(this);
    addTextChangedListener(this);
}


private void setClearIconVisible(boolean visible) {
    clearDrawable.setVisible(visible, false);
    setCompoundDrawables(null, null, visible? clearDrawable : null, null); //setCompoundPadding
}


@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    if(isFocused()){
        setClearIconVisible(s.length() > 0);
    }
}

@Override
public void afterTextChanged(Editable s) {

}

@Override
public void onFocusChange(View v, boolean hasFocus) {
    if(hasFocus)
        setClearIconVisible(getText().length() > 0);
    else
        setClearIconVisible(false);

    if(onFocusChangeListener != null)
        onFocusChangeListener.onFocusChange(v, hasFocus);
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    final int x = (int) event.getX();
    if(clearDrawable.isVisible() && x > getWidth() - getPaddingRight() - clearDrawable.getIntrinsicWidth()){
        if(event.getAction() == MotionEvent.ACTION_UP){
            setError(null);
            setText(null);
        }
        return true;
    }
    if(onTouchListener != null){
        return onTouchListener.onTouch(v, event);
    }
    return false;
}

}

最后,我的错误日志

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.fapa.brent.fapa, PID: 20853
java.lang.BootstrapMethodError: Exception from call site #1 bootstrap method
    at butterknife.internal.DebouncingOnClickListener.<clinit>(DebouncingOnClickListener.java:12)
    at com.fapa.brent.fapa.MainActivity_ViewBinding.<init>(MainActivity_ViewBinding.java:35)
    at java.lang.reflect.Constructor.newInstance0(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
    at butterknife.ButterKnife.bind(ButterKnife.java:171)
    at butterknife.ButterKnife.bind(ButterKnife.java:100)
    at com.fapa.brent.fapa.MainActivity.onCreate(MainActivity.java:46)
    at android.app.Activity.performCreate(Activity.java:7009)
    at android.app.Activity.performCreate(Activity.java:7000)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
 Caused by: java.lang.NoClassDefFoundError: Invalid descriptor: ENABLED_STATE_SET.
    at butterknife.internal.DebouncingOnClickListener.<clinit>(DebouncingOnClickListener.java:12) 
    at com.fapa.brent.fapa.MainActivity_ViewBinding.<init>(MainActivity_ViewBinding.java:35) 
    at java.lang.reflect.Constructor.newInstance0(Native Method) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:334) 
    at butterknife.ButterKnife.bind(ButterKnife.java:171) 
    at butterknife.ButterKnife.bind(ButterKnife.java:100) 
    at com.fapa.brent.fapa.MainActivity.onCreate(MainActivity.java:46) 
    at android.app.Activity.performCreate(Activity.java:7009) 
    at android.app.Activity.performCreate(Activity.java:7000) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6494) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

我删除时

ButterKnife.bind(this);

我的应用程序启动正常,但与此同时,我的应用程序崩溃并停止。似乎初始化存在问题,但是我找不到任何有用的信息

build.gradle

  apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.fapa.brent.fapa"
        minSdkVersion 26
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    implementation 'com.facebook.stetho:stetho:1.5.0'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.jakewharton:butterknife:9.0.0-rc2'
    annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0-rc2'
    implementation 'org.greenrobot:greendao:3.2.2'
}

2 个答案:

答案 0 :(得分:0)

由于该类与XML元素相关,您是否尝试将EditText中的MainActivity.java更改为自定义BrentEditText

所以

@BindView(R.id.pw)
EditText mPw;

@BindView(R.id.pw)
BrentEditText mPw;

答案 1 :(得分:0)

尝试一次降级ButterKnife的版本。有时可能由于gradle差异问题而发生。我在最新版本中也遇到了一些问题。甚至我也尝试过使用您的依赖项行,并且未进行编译。因此,这可能会有所帮助。如果它对您有用,请接受我的回答。