Android с использованием ButterKnife, проблема butterknife.bind (это) - PullRequest
0 голосов
/ 20 декабря 2018

У меня запущен собственный проект, и я использую 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 голосов
/ 23 декабря 2018

Попробуйте понизить версию ButterKnife один раз.Иногда это может произойти из-за разницы в Gradle.Я также столкнулся с проблемой в последней версии.Даже я попробовал это с вашими строками зависимости, и это не было скомпилировано.Так что, возможно, это поможет.Пожалуйста, примите мой ответ, если он работает для вас.

0 голосов
/ 21 декабря 2018

Вы пытались изменить EditText в MainActivity.java на свой пользовательский BrentEditText, поскольку этот класс относится к элементу XML?

Итак

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

на

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