Android studio使用androidannotations所出现的问题

最近在使用Android studio,用到了androidannotations的库,但是有一些坑,为了记录一下,以备忘记。

http://blog.csdn.net/sw950729/article/details/52901669

annotation的配置

project的build.gradle

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.5.0'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}
allprojects {
    repositories {
        jcenter()
    }
}
task clean(type: Delete) {
    delete rootProject.buildDir
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

moudle的build.gradle配置

apply plugin: 'com.android.application'
apply plugin: 'android-apt'
def AAVersion = '4.0+'
repositories {
    jcenter()
    flatDir {
        dirs 'libs'
    }
}
android {
    compileSdkVersion 19
    buildToolsVersion "23.0.2"
    defaultConfig {
        applicationId "source.code.watch.film"
        minSdkVersion 14
        targetSdkVersion 14
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    signingConfigs {
        debug {
            storeFile file("E:\\psd\\debug.keystore")
        }
    }
}
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    apt "org.androidannotations:androidannotations:$AAVersion"
    compile "org.androidannotations:androidannotations-api:$AAVersion"
}
apt {
    arguments {
        androidManifestFile variant.outputs[0].processResources.manifestFile
        resourcePackageName 'source.code.watch.film'
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

今天和大家介绍下Android的懒人框架:annotation。

注意:很多人都添加了

上面的37行到42行的apt代码,导致不能使用,出现Cannot get property 'processResources' on null object 的错误,

其实只要把这几行代码删除就行了。但是之后还有问题,

The AndroidManifest.xml file contains the original component, and not the AndroidAnnotations generated component. Please register LoginActivity_ instead of LoginActivity

会报出这个问题,其实这个问题很蛋疼,只需要把原来的清单配置文件中的activity名字后面添加个“_”就行了。


之前名字是MainActivity,我给换成了“MainActivity_”.就好了。

见网址:http://www.4byte.cn/question/1068592/androidannotations2-7-1-androidmanifest-xml-file-contains-the-original-component.html

@EActivity

@EActivity(R.layout.main)
public class MyActivity extends Activity {
}
 
 
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

@EFragment

@EFragment(R.layout.my_fragment_layout)
public class MyFragment extends Fragment {
}
 
 
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
<fragment
        android:id="@+id/myFragment"
        android:name="com.company.MyFragment_"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5
MyFragment fragment = new MyFragment_();
 
 
  • 1
  • 1

@EBean

public class MyClass {
}
- 这个类必须仅仅只能有一个构造函数,参数最多有一个context
 
 
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
@EActivity(R.layout.main)
public class MyActivity extends Activity {
  @Bean
  MyClass myClass;
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5
@EActivity(R.layout.main)
public class MyActivity extends Activity {
  @Bean(MyClass.class)
  MyClassInterface myClassInterface;
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5
@EBean
public class MyClass {
  @RootContext
  Context context;
  @RootContext
  Activity activity;
  @RootContext
  Service service;
  @RootContext
  MyActivity myActivity;
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
@EActivity(R.layout.main)
public class MyActivity extends Activity {
  @AfterInject
  public void doSomethingAfterInjection() {
  }
}
- 如果想在类创建时期做一些操作可以这么做
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
@EBean(scope = Scope.Singleton)
public class MySingleton {
}
- 单例类
- 在单例类里面不可以注入view和事件绑定,因为单例的生命周期比Activity和Service的要长,以免发生内存溢出
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

@EView

@EView
public class CustomButton extends Button {
  @App
  MyApplication application;
  @StringRes
  String someStringResource;
  public CustomButton(Context context, AttributeSet attrs) {
    super(context, attrs);
  }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
<com.androidannotations.view.CustomButton_
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
 
 
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
CustomButton button = CustomButton_.build(context);
 
 
  • 1
  • 1

@EViewGroup

@EViewGroup(R.layout.title_with_subtitle)
public class TitleWithSubtitle extends RelativeLayout {
  @ViewById
  protected TextView title, subtitle;
  public TitleWithSubtitle(Context context, AttributeSet attrs) {
    super(context, attrs);
  }
  public void setTexts(String titleText, String subTitleText) {
    title.setText(titleText);
    subtitle.setText(subTitleText);
  }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
<com.androidannotations.viewgroup.TitleWithSubtitle_
        android:id="@+id/firstTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
 
 
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

@EAplication

@EApplication
public class MyApplication extends Application {
}
 
 
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
@EActivity(R.layout.main)
public class MyActivity extends Activity {
  @App
  MyApplication application;
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

EService

@EService
public class MyService extends Service {
}
 
 
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
MyService_.intent(getApplication()).start();
 
 
  • 1
  • 1
MyService_.intent(getApplication()).stop();
 
 
  • 1
  • 1

@EReceiver

@EReceiver
public class MyReceiver extends BroadcastReceiver {
}
 
 
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
@EActivity(R.layout.main)
public class MyActivity extends Activity {
  @Receiver(actions = "org.androidannotations.ACTION_1")
  protected void onAction1() {
  }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

@EProvider

@EProvider
public class MyContentProvider extends ContentProvider {
}
 
 
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

@EViewById


@EActivity(R.layout.main)
public class MyActivity extends Activity {
  @ViewById
  EditText myEditText;
  @ViewById(R.id.myTextView)
  TextView textView;
}
- 没有括号里定义的变量名称必须和布局的id名称一致
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

@EAfterViews

@EActivity(R.layout.main)
public class MyActivity extends Activity {
   @ViewById
   TextView myTextView;
   @AfterViews
   void updateTextWithDate() {
      myTextView.setText("Date: " + new Date());
   }
}
 一定要在这里进行view的一些设置,不要在oncreate()中设置,因为oncreate()在执行时 view还没有注入
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

@StringRes

@EActivity(R.layout.main)
public class MyActivity extends Activity {
   @StringRes(R.string.hello)
   String myHelloString;
   @StringRes
   String hello;
}
- 不能将变量设置成私有变量
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

@ColorRes

@EActivity(R.layout.main)
public class MyActivity extends Activity {
  @ColorRes(R.color.backgroundColor)
  int someColor;
  @ColorRes
  int backgroundColor;
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

@AnimationRes

@EActivity(R.layout.main)
public class MyActivity extends Activity {
   @AnimationRes(R.anim.fadein)
   XmlResourceParser xmlResAnim;
   @AnimationRes
   Animation fadein;
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

@DimensionRes

@EActivity(R.layout.main)
public class MyActivity extends Activity {
  @DimensionRes(R.dimen.fontsize)
  float fontSizeDimension;
  @DimensionRes
  float fontsize;
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

@DimensionPixelOffsetRes

@EActivity(R.layout.main)
public class MyActivity extends Activity {
  @DimensionPixelOffsetRes(R.string.fontsize)
  int fontSizeDimension;
  @DimensionPixelOffsetRes
  int fontsize;
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

@DimensionPixelSizeRes

@EActivity(R.layout.main)
public class MyActivity extends Activity {
  @DimensionPixelSizeRes(R.string.fontsize)
  int fontSizeDimension;
  @DimensionPixelSizeRes
  int fontsize;
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

@Extra

@EActivity(R.layout.main)
public class MyActivity extends Activity {
  @Extra("myStringExtra")
  String myMessage;
  @Extra("myDateExtra")
  Date myDateExtraWithDefaultValue = new Date();
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
@EActivity(R.layout.main)
public class MyActivity extends Activity {
  @Extra
  String myMessage;
}
- The name of the extra will be "myMessage",名字必须一致
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
MyActivity_.intent().myMessage("hello").start() ;
 
 
  • 1
  • 1
MyActivity_.intent().myMessage("hello").startForResult() ;
 
 
  • 1
  • 1

@SystemService

@EActivity(R.layout.main)
public class MyActivity extends Activity {
  @SystemService
  NotificationManager notificationManager;
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

@HtmlRes

@EActivity(R.layout.main)
public class MyActivity extends Activity {
  @HtmlRes(R.string.hello_html)
  Spanned myHelloString;
  @HtmlRes
  CharSequence helloHtml;
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

@FromHtml

@EActivity(R.layout.main)
public class MyActivity extends Activity {
  @ViewById(R.id.my_text_view)
  @FromHtml(R.string.hello_html)
  TextView textView;
  @ViewById
  @FromHtml
  TextView hello_html;
}
- 必须用在TextView
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

@NonConfigurationInstance

@EActivity(R.layout.main)
public class MyActivity extends Activity {
  @NonConfigurationInstance
  Bitmap someBitmap;
  @NonConfigurationInstance
  @Bean
  MyBackgroundTask myBackgroundTask;
}
- 等同于Activity.onRetainNonConfigurationInstance()
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

@Click

@Click(R.id.myButton)
void myButtonWasClicked() {
}
@Click
void anotherButton() {
}
@Click
void yetAnotherButton(View clickedView) {
}
- LongClick和这个类似
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

@SeekBarProgressChange


@SeekBarProgressChange(R.id.seekBar)
 void onProgressChangeOnSeekBar(SeekBar seekBar, int progress, boolean fromUser) {
 }
 @SeekBarProgressChange(R.id.seekBar)
 void onProgressChangeOnSeekBar(SeekBar seekBar, int progress) {
 }
 @SeekBarProgressChange({R.id.seekBar1, R.id.seekBar2})
 void onProgressChangeOnSeekBar(SeekBar seekBar) {
 }
 @SeekBarProgressChange({R.id.seekBar1, R.id.seekBar2})
 void onProgressChangeOnSeekBar() {
 }
- @SeekBarTouchStart 和 @SeekBarTouchStop 接受开始和结束事件的监听
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

@TextChange

@TextChange(R.id.helloTextView)
 void onTextChangesOnHelloTextView(CharSequence text, TextView hello, int before, int start, int count) {
 }
 @TextChange
 void helloTextViewTextChanged(TextView hello) {
 }
 @TextChange({R.id.editText, R.id.helloTextView})
 void onTextChangesOnSomeTextViews(TextView tv, CharSequence text) {
 }
 @TextChange(R.id.helloTextView)
 void onTextChangesOnHelloTextView() {
 }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

@BeforeTextChange

@BeforeTextChange(R.id.helloTextView)
 void beforeTextChangedOnHelloTextView(TextView hello, CharSequence text, int start, int count, int after) {
 }
 @BeforeTextChange
 void helloTextViewBeforeTextChanged(TextView hello) {
 }
 @BeforeTextChange({R.id.editText, R.id.helloTextView})
 void beforeTextChangedOnSomeTextViews(TextView tv, CharSequence text) {
 }
 @BeforeTextChange(R.id.helloTextView)
 void beforeTextChangedOnHelloTextView() {
 }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

@AfterTextChange

@AfterTextChange(R.id.helloTextView)
 void afterTextChangedOnHelloTextView(Editable text, TextView hello) {
 }
 @AfterTextChange
 void helloTextViewAfterTextChanged(TextView hello) {
 }
 @AfterTextChange({R.id.editText, R.id.helloTextView})
 void afterTextChangedOnSomeTextViews(TextView tv, Editable text) {
 }
 @AfterTextChange(R.id.helloTextView)
 void afterTextChangedOnHelloTextView() {
 }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

@Background

void myMethod() {
    someBackgroundWork("hello", 42);
}
@Background
void someBackgroundWork(String aParam, long anotherParam) {
}
- 后台运行
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
void myMethod() {
    someCancellableBackground("hello", 42);
    boolean mayInterruptIfRunning = true;
    BackgroundExecutor.cancelAll("cancellable_task", mayInterruptIfRunning);
}
@Background(id="cancellable_task")
void someCancellableBackground(String aParam, long anotherParam) {
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
void myMethod() {
    for (int i = 0; i < 10; i++)
        someSequentialBackgroundMethod(i);
}
@Background(serial = "test")
void someSequentialBackgroundMethod(int i) {
    SystemClock.sleep(new Random().nextInt(2000)+1000);
    Log.d("AA", "value : " + i);
}
- 非并发执行
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
@Background(delay=2000)
void doInBackgroundAfterTwoSeconds() {
}
- 延迟执行
 
 
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

@UIThread

void myMethod() {
    doInUiThread("hello", 42);
}
@UiThread
void doInUiThread(String aParam, long anotherParam) {
}
- UI线程
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
@UiThread(delay=2000)
void doInUiThreadAfterTwoSeconds() {
}
- 延迟
 
 
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4
@UiThread(propagation = Propagation.REUSE)
void runInSameThreadIfOnUiThread() {
}
- 优化UI线程
 
 
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4
@EActivity(R.layout.main)
public class MyActivity extends Activity {
  @Background
  void doSomeStuffInBackground() {
    publishProgress(0);
    publishProgress(10);
    publishProgress(100);
  }
  @UiThread
  void publishProgress(int progress) {
  }
}
- 后台向UI线程传值
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

@OnActivityResult

@OnActivityResult(REQUEST_CODE)
void onResult(int resultCode, Intent data) {
}
@OnActivityResult(REQUEST_CODE)
void onResult(int resultCode) {
}
@OnActivityResult(ANOTHER_REQUEST_CODE)
void onResult(Intent data) {
}
@OnActivityResult(ANOTHER_REQUEST_CODE)
void onResult() {
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

好了。关于annotation的介就是这么多。欢迎进群交流。还有,求工作啊!!!!


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值