OnClick linstener using Butterknife is not working when i click button it's not happening any thing,someone please check the code below and suggest me any modifications if any.
MainActivity.java
package com.example.niranjan.sample;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class MainActivity extends AppCompatActivity {
#BindView(R.id.button)
Button button;
#BindView(R.id.edit)
EditText edit;
#BindView(R.id.text)
TextView text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
#OnClick(R.id.button)
public void onClick(View view)
{
String text1 = edit.getText().toString();
text.setText(text1);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.niranjan.sample.MainActivity">
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="addtext" />
</LinearLayout>
build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.2"
defaultConfig {
applicationId "com.example.niranjan.sample"
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(dir: 'libs', include: ['*.jar'])
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.1'
compile 'com.jakewharton:butterknife:8.4.0'
testCompile 'junit:junit:4.12'
}
I done below steps and successfully implemented butter knife
In build.gradle(project)
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}
apply the 'android-apt' and dependency in build.gradle(app)
apply plugin: 'android-apt'
android {
...
}
dependencies {
compile 'com.jakewharton:butterknife:8.4.0'
apt 'com.jakewharton:butterknife-compiler:8.4.0'
}
I had the same problem, try this in your build.gradle(Module.app)
compile 'com.jakewharton:butterknife:8.4.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
and then
apt 'com.jakewharton:butterknife-compiler:8.4.0'
also do not forget
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' in build.gradle(Project)
You neeed to add apt 'com.jakewharton:butterknife-compiler:8.4.0' under apply plugin in build.gradle
Also add
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
In your project build.gradle inside buildScript->dependencies
Related
I am trying to use AdblockWebView library to block ads on Android webview. I tried to connect with my phone but the developed app crashes. I am using the following code. Is there anything I should change.
MainActivit.java
import org.adblockplus.libadblockplus.android.webview.AdblockWebView;
public class MainActivity extends AppCompatActivity {
AdblockWebView superWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
superWebView = findViewById(R.id.main_webview);
superWebView.getSettings().setJavaScriptEnabled(true);
superWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
superWebView.getSettings().setDomStorageEnabled(true);
superWebView.setWebViewClient(new WebViewClient());
superWebView.clearCache(true);
superWebView.loadUrl("https://m.youtube.com/");
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<org.adblockplus.libadblockplus.android.webview.AdblockWebView
android:id="#+id/main_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
build.gradle (project)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
build.gradle (Module)
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.example.webview"
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation 'org.adblockplus:adblock-android-webview:3.14'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
Your issue is most likely coming from superWebView.clearCache(true);
Remove this line and the app should run.
I want to use pulseLayout In my Project but it's not working I made Demo Project for Find out Reason for it not able to conclude anything. I tried 2 pulse lib but none of them are working.
lib are : https://github.com/gaurav414u/android-ripple-pulse-animation
https://github.com/booncol/Pulsator4Droid
Layout File :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
xmlns:ripple="http://schemas.android.com/tools"
tools:context=".MainActivity">
<com.gauravbhola.ripplepulsebackground.RipplePulseLayout
android:layout_width="200dp"
android:layout_height="200dp"
android:clipChildren="false"
ripple:rippleColor="#3D66C7"
ripple:rippleType="stroke"
ripple:strokeWidth="2dp"
ripple:startRadius="42dp"
android:layout_centerInParent="true"
ripple:endRadius="100dp"
ripple:duration="2000"
android:id="#+id/layout_ripplepulse">
</com.gauravbhola.ripplepulsebackground.RipplePulseLayout>
<pl.bclogic.pulsator4droid.library.PulsatorLayout
android:id="#+id/pulsator"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:pulse_count="4"
app:pulse_duration="7000"
app:pulse_repeat="0"
android:layout_below="#+id/layout_ripplepulse"
app:pulse_color="#color/colorAccent"
app:pulse_startFromScratch="false"
app:pulse_interpolator="Linear">
</pl.bclogic.pulsator4droid.library.PulsatorLayout>
</RelativeLayout>
Activity :
public class MainActivity extends AppCompatActivity {
RipplePulseLayout layout_ripplepulse;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout_ripplepulse =findViewById(R.id.layout_ripplepulse);
layout_ripplepulse.startRippleAnimation();
}
}
App level Gradle :
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.trueapps.wordsearchnewproject"
minSdkVersion 16
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
implementation 'pl.bclogic:pulsator4droid:1.0.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.gauravbhola.ripplepulsebackground:library:1.0.0'
}
Project level Gradle :
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
In the Xml file header you are using
xmlns:ripple="http://schemas.android.com/tools"
Just replace it with
xmlns:ripple="http://schemas.android.com/apk/res-auto"
im having issue with this error "error: cannot find symbol class DataBindingComponent" from app/build/generated/source/dataBinding/baseClasses/debug . i have tried all the options people suggested on internet but no help at all.
these options included:
1. clean / rebuild project
2. invalidate caches / restart
1) this is my root build.gradle
buildscript {
repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
classpath 'com.google.gms:google-services:4.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven { url "https://jitpack.io" }
maven {
url "https://maven.google.com"
}
}
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xmaxerrs" << "500"
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
ext {
// Sdk and tools
minSdkVersion = 17
devMinSdkVersion = 21;
devPreLollipopSdkVersion = 19;
targetSdkVersion = 27
compileSdkVersion = 27
buildToolsVersion = '27.0.3'
// App dependency
supportLibraryVersion = '27.1.1'
retrofit = "2.3.0"
okHttp = "3.5.0"
butterknife= "8.8.1"
glide = "4.7.1"
glideCompiler = "4.7.1"
rxJava = '2.1.8'
rxAndroid = '2.0.1'
dagger2 = "2.16"
// version
versionCode = 1
versionName = "1.0"
}
2) this is my module build.gradle
`apply plugin: 'com.android.application'
android {
dataBinding {
enabled = true
}
compileSdkVersion rootProject.ext.targetSdkVersion
dexOptions {
maxProcessCount 2
javaMaxHeapSize "2g"
}
defaultConfig {
applicationId "com.movecrop.shipper"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode rootProject.ext.versionCode
versionName rootProject.ext.versionName
multiDexEnabled true
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
debug {
minifyEnabled false
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
flavorDimensions "movecrop.shipper"
compileOptions {
targetCompatibility 1.8
sourceCompatibility 1.8
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"
implementation "com.android.support:support-v4:$rootProject.supportLibraryVersion"
implementation "com.android.support:customtabs:$rootProject.supportLibraryVersion"
implementation "com.android.support:design:$rootProject.supportLibraryVersion"
implementation "com.android.support:cardview-v7:$rootProject.supportLibraryVersion"
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'com.android.support:multidex:1.0.3'
// glide
implementation "com.github.bumptech.glide:glide:$rootProject.glide"
annotationProcessor "com.github.bumptech.glide:compiler:$rootProject.glideCompiler"
implementation 'jp.wasabeef:glide-transformations:2.0.2'
// retrofit
implementation "com.squareup.retrofit2:retrofit:$rootProject.retrofit"
implementation "com.squareup.retrofit2:retrofit-mock:$rootProject.retrofit"
implementation "com.squareup.retrofit2:converter-gson:$rootProject.retrofit"
implementation "com.squareup.retrofit2:adapter-rxjava2:$rootProject.retrofit"
// okhttp3
implementation "com.squareup.okhttp3:okhttp:$rootProject.okHttp"
implementation "com.squareup.okhttp3:logging-interceptor:$rootProject.okHttp"
implementation 'com.squareup.okio:okio:1.13.0'
implementation 'de.hdodenhof:circleimageview:2.1.0'
implementation 'com.orhanobut:hawk:2.0.1'
implementation 'com.jakewharton.byteunits:byteunits:0.9.1'
implementation 'com.github.tajchert:nammu:1.2.0'
// RxJava
implementation "io.reactivex.rxjava2:rxjava:$rootProject.ext.rxJava"
implementation "io.reactivex.rxjava2:rxandroid:$rootProject.ext.rxAndroid"
// Bottom navigation
implementation 'it.sephiroth.android.library.bottomnavigation:bottom-navigation:2.0.1-rc1'
// curl
implementation 'com.github.mrmike:Ok2Curl:master-SNAPSHOT'
// Firebase
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.google.firebase:firebase-messaging:17.0.0'
implementation 'com.firebase:firebase-jobdispatcher:0.8.5'
// Dagger2
implementation "com.google.dagger:dagger-android:$rootProject.dagger2"
// Dagger2 if you use the support libraries
implementation "com.google.dagger:dagger-android-support:$rootProject.dagger2"
annotationProcessor "com.google.dagger:dagger-android-processor:$rootProject.dagger2"
annotationProcessor "com.google.dagger:dagger-compiler:$rootProject.dagger2"
def lifecycle_version = "1.1.1"
implementation "android.arch.lifecycle:livedata:$lifecycle_version"
def room_version = "1.1.1"
implementation "android.arch.persistence.room:runtime:$room_version"
annotationProcessor "android.arch.persistence.room:compiler:$room_version"
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
apply plugin: 'com.google.gms.google-services'
`
Hope you guys can give me some suggestion, thanks
updated:
This is one of my activity (and its layout xml) that show error:
package com.movecrop.shipper.ui.home;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingComponent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class HomeActivity
extends AppCompatActivity
implements HomeViewModel.HomeListener,
SwitchWorkingStatusReceiver.OnSwitchWorkingStatusListener {
private HomeViewModel mHomeViewModel;
ActivityHomeBinding mBinding;
LayoutDrawerHeaderBinding mHeaderBinding;
private ActionBarDrawerToggle mActionBarDrawerToggle;
private SwitchWorkingStatusReceiver mSwitchWorkingStateReceiver = new SwitchWorkingStatusReceiver();
private static final String TAG = "HomeActivity";
public static Intent newIntent(Context context) {
Intent i = new Intent(context, HomeActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
return i;
}
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Injector.getInstance().component().inject(this);
mBinding = DataBindingUtil.setContentView(this, R.layout.activity_home);
mBinding.setHomeActivity(this);
mHomeViewModel = new HomeViewModel();
mHomeViewModel.initialize(this);
LocalBroadcastManager.getInstance(this).registerReceiver(
mSwitchWorkingStateReceiver,
SwitchWorkingStatusReceiver.getIntenFilter()
);
initDrawer();
mHomeViewModel.loadUser();
navigate(R.id.menu_dashboard);
}
}
<layout 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"
tools:context="com.movecrop.shipper.ui.home.HomeActivity">
<data>
<variable
name="homeActivity"
type="com.movecrop.shipper.ui.home.HomeActivity" />
</data>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:actionBarSize"
app:title="#string/label_dashboard_title" />
<ImageView
android:id="#+id/imgNotification"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="right|center_vertical"
android:cropToPadding="true"
android:padding="12dp"
android:visibility="invisible" />
</FrameLayout>
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/drawerNavigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/white"
android:theme="#style/ThemeOverlay.AppCompat.Light"
app:headerLayout="#layout/layout_drawer_header"
app:itemBackground="#drawable/bg_menu_item_selector"
app:itemIconTint="#color/color_menu_item_selector"
app:itemTextColor="#color/colorDrawerIconTint"
app:menu="#menu/drawer">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="vertical">
<Button
android:id="#+id/btnToggle"
android:layout_width="wrap_content"
android:layout_height="#dimen/button_height"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="32dp"
android:layout_marginRight="32dp"
android:layout_marginTop="40dp"
android:background="#drawable/bg_primary_button_unselected"
android:maxLines="1"
android:onClick="#{homeActivity::click}"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:text="#string/button_go_online"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textSize="12sp" />
<TextView
android:id="#+id/btnSetting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginTop="32dp"
android:background="#drawable/ts_list_item"
android:drawableLeft="#drawable/ic_dm_setting"
android:drawablePadding="32dp"
android:onClick="#{homeActivity::click}"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:textColor="#color/colorDrawerIconTint"
android:textStyle="bold"
android:fitsSystemWindows="true"
android:text="#string/dm_setting" />
</LinearLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
</layout>
Turns out the error comes from my dao interface for room database not dagger2 neither databinding library. hope it'll help anyone who has same problem.
I have 3 modules in my project: common, admin and client.
The first is an Android Library with an abstract LoginActivity that the other modules (apps) extends from.
LoginActivity has its own layout and all methods implemented but one: tryLogin(user, password) that navigates to the main activity, which is different for each app.
I have followed the official documentation but when I click on signIn button the OnClickListener is not called. Moreover, all bound views are null...
This are all related code from admin (both modules are almost identical)
Project level build.gradle
buildscript {
repositories {
jcenter()
mavenCentral()
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
classpath "io.realm:realm-gradle-plugin:1.1.1"
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
classpath 'com.jakewharton:butterknife-gradle-plugin:8.2.1'
classpath 'io.fabric.tools:gradle:1.21.7'
}
}
allprojects {
repositories {
jcenter()
maven { url 'https://maven.fabric.io/public' }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Common library build.gradle
apply plugin: 'com.android.library'
apply plugin: 'realm-android'
apply plugin: 'com.jakewharton.butterknife'
apply plugin: 'android-apt'
android {
compileSdkVersion 24
buildToolsVersion "24.0.1"
defaultConfig {
minSdkVersion 17
targetSdkVersion 24
versionCode 1
versionName "0.1"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
// Local
compile fileTree(dir: 'libs', include: ['*.jar'])
...
compile 'com.jakewharton:butterknife:8.2.1'
}
Admin app build.gradle
apply plugin: 'com.android.application'
apply plugin: 'realm-android'
apply plugin: 'io.fabric'
apply plugin: 'android-apt'
android {
compileSdkVersion 24
buildToolsVersion "24.0.1"
defaultConfig {
applicationId "..."
minSdkVersion 17
targetSdkVersion 24
versionCode 1
versionName "0.1"
}
buildTypes {
debug {
applicationIdSuffix '.debug'
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
...
// Butterknife
compile 'com.jakewharton:butterknife:8.2.1'
apt 'com.jakewharton:butterknife-compiler:8.2.1'
}
LoginActivity (common)
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.Button;
import android.widget.EditText;
import xxx.common.R;
import xxx.common.R2;
import butterknife.BindView;
import butterknife.OnClick;
public abstract class LoginActivity extends BaseActivity {
#BindView(R2.id.login_user_form)
protected EditText user;
#BindView(R2.id.login_password_form)
protected EditText password;
#BindView(R2.id.login_sign_in)
protected Button signIn;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
}
#OnClick(R2.id.login_sign_in)
protected void onSignInClick() {
String user = this.user.toString();
String password = this.password.getText().toString();
/* Fields validations ... */
tryLogin(user, password);
}
protected abstract void tryLogin(String user, String password);
}
activity_login.xml (common)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true"
android:orientation="vertical"
android:padding="#dimen/activity_horizontal_margin">
<ImageView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="0.4"
android:scaleType="fitCenter"
android:src="#drawable/brand" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.6"
android:gravity="center_horizontal"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:id="#+id/login_user_form"
style="#style/LoginEditText" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:id="#+id/login_password_form"
style="#style/LoginEditText" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/login_sign_in"
style="#style/LoginButton"
android:text="#string/login_sign_in" />
</LinearLayout>
</LinearLayout>
AdminLoginActivity (admin)
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import xxx.activity.LoginActivity;
import butterknife.ButterKnife;
public class AdminLoginActivity extends LoginActivity {
#Override
protected void tryLogin(String user, String password) {
// This is never called!!
Toast.makeText(this, "Login in...", Toast.LENGTH_SHORT).show();
}
}
Te application is not crashing or anything, it does just nothing.
What am I doing wrong??
My guess is that the code has the wrong R file being imported.
To verify this, you can look at the R file that is automagically built in the build/generated/source of the library module in issue and see if the id your looking for is included. There are several R files generated. Find the one that has the id your looking for and make sure it matches your R import namespace.
I'm trying to use Butter Knife in a custom adapter like the documentation advertises: http://jakewharton.github.io/butterknife/
I pasted the example code and inserted the layout with my RelativeLayout file:
#Override public View getView(int position, View view, ViewGroup parent) {
ViewHolder holder;
ButterKnife.setDebug(true);
if (view != null) {
holder = (ViewHolder) view.getTag();
} else {
view = inflater.inflate(R.layout.list_row_people, parent, false);
holder = new ViewHolder(view);
view.setTag(holder);
}
holder.name.setText("John Doe");
// etc...
return view;
}
This is the code of the ViewHolder (inline class of PeopleAdapter):
static class ViewHolder {
#BindView(R.id.name)
TextView name;
public ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
Unfortunately, holder.name.setText("John Doe"); throws a NullPointer exceptions as name is null. The debug output shows these lines:
13:11:32.816 11613-11613/com.myproject.debug D/ButterKnife: Looking up view binder for com.myproject.controller.adapters.PeopleAdapter$ViewHolder
13:11:32.827 11613-11613/com.myproject.debug D/ButterKnife: Not found. Trying superclass java.lang.Object
13:11:32.827 11613-11613/com.myproject.debug D/ButterKnife: MISS: Reached framework class. Abandoning search.
list_row_people.xml currently looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center_vertical"
android:padding="4dp"
android:text="#string/example_name"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Reading https://github.com/JakeWharton/butterknife/issues/285, I added the following line to my proguard-rules.pro:
-keep class **$$ViewBinder { *; }
Edit:
Android Studio 2.1 with com.jakewharton:butterknife:8.0.1. This is my build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'android-apt'
apply plugin: 'kotlin-android'
repositories {
mavenCentral()
}
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.myproject"
minSdkVersion 18
targetSdkVersion 23
versionCode 18
versionName "0.0.16"
multiDexEnabled true
}
packagingOptions {
exclude 'main/AndroidManifest.xml'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
applicationIdSuffix ".debug"
}
}
lintOptions {
abortOnError false
warning 'InvalidPackage'
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
compileOptions {
targetCompatibility JavaVersion.VERSION_1_7
sourceCompatibility JavaVersion.VERSION_1_7
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
wearApp project(':wear')
testCompile 'junit:junit:4.12'
apt 'com.github.hotchemi:permissionsdispatcher-processor:2.1.2'
releaseCompile project(path: ':common', configuration: 'release')
debugCompile project(path: ':common', configuration: 'debug')
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile 'com.google.android.gms:play-services-maps:8.4.0'
compile 'com.google.maps.android:android-maps-utils:0.4.3'
compile 'com.google.android.gms:play-services-wearable:8.4.0'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:support-v4:23.3.0'
compile 'com.android.support:design:23.3.0'
compile 'com.android.support:recyclerview-v7:23.3.0'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.jakewharton.timber:timber:2.5.0'
compile 'com.jakewharton:butterknife:8.0.1'
compile 'com.github.hotchemi:permissionsdispatcher:2.1.2'
compile 'com.squareup.picasso:picasso:2.5.2'
}
buildscript {
ext.kotlin_version = '1.0.1-2'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
In Butter Knife from Version 8.0.0.
The Runtime and compiler are now split into two artifacts.
compile 'com.jakewharton:butterknife:8.0.0'
apt 'com.jakewharton:butterknife-compiler:8.0.0'
Look at this Link also.