Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Butter Knife won't inject

Tags:

butterknife

I'm trying to use Butter Knife at Android Studio these days, yet it fails me with some odd problems. I used to try Butter Knife at eclipse, and it worked fine. I don't know if I configured something wrong at Android Studio.

I followed instructions at Jake Wharton's website, yet it's still not working. At first, It could be compiled and installed, but after Butterknife.inject(this), the view would still be null, and it threw a null pointer error. Then I googled how to configure Butterknife at Android Studio, and followed this issue at stackoverflow, it could not pass compilation then.

Then I reverted to my first version of configuration, it also began to fail compilation. Here's the message:

Error:(14, 12): @InjectView-annotated class incorrectly in Android framework package. (android.rocky.butterknife_test.MyActivity)

and

Error:Execution failed for task ':app:compileDebugJava'. Compilation failed; see the compiler error output for details.


This is my configuration of app/build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 20
    buildToolsVersion "20.0.0"

    defaultConfig {
        applicationId "android.rocky.butterknife_test"
        minSdkVersion 19
        targetSdkVersion 20
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            runProguard false
        }
    }
    lintOptions {
        disable 'InvalidPackage'
    }
    packagingOptions {
        exclude 'META-INF/services/javax.annotation.processing.Processor'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.jakewharton:butterknife:5.1.2'
}

This is my configuration of build.gradle at root:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.12.+'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        mavenCentral()
    }
}

This is my Java code:

public class MyActivity extends Activity {
  @InjectView(R.id.greeting)
  TextView greetingTextView;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    ButterKnife.inject(this);
    greetingTextView.setText("butter knife!");
  }


  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.my, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
      return true;
    }
    return super.onOptionsItemSelected(item);
  }
}

BTW, my android studio version is 0.8.2. If you think the snippets I paste are all fine, tell me to paste other snippets. Thanks a lot!

like image 646
misaka-10032 Avatar asked Mar 18 '23 20:03

misaka-10032


1 Answers

The exception says it all:

@InjectView-annotated class incorrectly in Android framework package. (android.rocky.butterknife_test.MyActivity)

Your class is in the Android framework package, android.*. Applications should not use the android.* or java.* packages for their code. ButterKnife will eagerly reject these packages.

like image 56
Jake Wharton Avatar answered May 12 '23 06:05

Jake Wharton