本文介绍了Android Studio:集成 Butterknife?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将 Butterknife 实施到我的 android studio 项目中.
I am trying to implement Butterknife into my android studio project.
但是,当我这样做时,我在 @InjectView
上收到错误消息无法解析符号 InjectView".
However when I do so I get an error on @InjectView
"cannot resolve symbol InjectView".
我没有成功实施 Butterknife 吗?
Have I not implemented Butterknife sucsessfully?
活动代码:
package com.example.rodf.testapp;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
@InjectView(R.id.tvHelloWorld) TextView tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:id="@+id/tvHelloWorld"
android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
gradle 文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.example.rodf.testapp"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
//adding the butter knife library
compile 'com.jakewharton:butterknife:6.0.0'
}
推荐答案
我觉得你的代码很好,
尝试通过点击
Try to sync your gradle by click
尝试转到 File
-- invalidate Caches
并重新启动您的 Android Studio.
Try to go File
-- invalidate Caches
and restart your Android studio.
另外,不要忘记把 ButterKnife.inject(this);
放在 onCreate()
Also, don't forget put ButterKnife.inject(this);
in onCreate()
这篇关于Android Studio:集成 Butterknife?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!