Injected views are null with RG 3.0

我的梦境 提交于 2019-12-24 05:34:09

问题


I am using RoboGuice 3.0 in Android Studio directly since I need support for ActionBarActivity. This is my dep:

compile 'org.roboguice:roboguice:3.0'

First odd thing I had to do was to resolve a ClassNotFoundException for "Unable to use annotation database(s)". It seems like if there's no annotation package given, the packageList is prepended with empty string and DI framework complains that it could not find AnnotationDatabaseImpl at root package (which is expected). So I did this in my manifest:

<meta-data android:name="roboguice.annotations.packages" android:value="roboguice"/>

which resolved the issue. Then, I changed my code to this:

@ContentView(R.layout.activity_playlists)
public class Playlists extends RoboActionBarActivity {
  @InjectView(R.id.toolbar)
  Toolbar toolbar;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setSupportActionBar(toolbar);
  }
}

setSupportActionBar() throws a NPE. I stepped through the debugger and viewMembersInjectors map is empty when the views are being injected. I am thinking the odd fix I had to do and this is related. Somehow the injectview member is not discovered. However my @ContentView injection works (if I remove it content changed callback is never called).

Does anyone know how to fix this?


回答1:


One solution would be with a custom class that extends the Application.class you could disable the annotation database usage.

public class CustomApplication extends Application {
    public void onCreate() {
        super.onCreate();
        RoboGuice.setUseAnnotationDatabases(false);
    }
}

Another way is to create an Enviroment variable named roboguice.useAnnotationDatabases with it's value set to false since RG3 seems to check for the value.

This will allow you to use RoboGuice like before, though it will lose all the performance gains from the use of the annotation database usage.


Update

Generating an annotation database should also fix your NPE and also will add some performance gains to your application. To do so first you have to include the following dependency in your build.gradle.

depencencies {
...
    provided 'org.roboguice:roboblender:3.0'
}

You should also add the following:

allprojects {
    gradle.projectsEvaluated {
        tasks.withType(JavaCompile) {
            options.compilerArgs << "-AguiceAnnotationDatabasePackageName=databasename"
        }
    }
}

To get your database working you should add databasename in the meta-data value in your AndroidManifest.xml.

 <meta-data android:name="roboguice.annotations.packages" android:value="roboguice,databasename"/>


来源:https://stackoverflow.com/questions/26555970/injected-views-are-null-with-rg-3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!