注入的观点是空与RG 3.0 [英] Injected views are null with RG 3.0

查看:145
本文介绍了注入的观点是空与RG 3.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android的Studio中使用RoboGuice 3.0直接,因为我需要ActionBarActivity支持。这是我的出发:

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

编译org.roboguice:roboguice:3.0

首先奇怪的事情我必须做的是解决了无法使用注释数据库(S)一个ClassNotFoundException。好像如果有没有给出注解包,packageList是<一个href=\"https://github.com/roboguice/roboguice/blob/master/roboguice/src/main/java/roboguice/RoboGuice.java#L278\"相对=nofollow> $ P与空字符串和DI框架ppended $抱怨说,它无法找到根包AnnotationDatabaseImpl(预计)。所以,我做这在我的清单:

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"/>

这解决了问题。然后,我改变了我的code这样:

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()抛出NPE。我通过调试和加强
当被注入的意见viewMembersInjectors映射为空。我想到的是奇修复我不得不这样做,这是相关的。不知怎的,injectview成员没有发现。但是我@ContentView注入工作(如果我删除其含量变化回调永远不会被调用)。

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?

推荐答案

一个解决方案是与扩展Application.class您可以禁用注释数据库使用的自定义类。

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);
    }
}

另一种方法是创建一个名为一个环境变量 roboguice.useAnnotationDatabases 与它的值设置为false,因为皂甙Rg3似乎检查值。

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.

这将允许您使用RoboGuice像以前一样,虽然会失去从使用注释数据库使用的所有的性能提升。

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.

更新

生成注释数据库还应该解决您的NPE,也将增加一些性能提升您的应用程序。要做到这一点首先要包含以下依赖你的的build.gradle

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"
        }
    }
}

要得到你的数据库的工作,你应该在元数据增值数据库名称的的Andr​​oidManifest.xml

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"/>

这篇关于注入的观点是空与RG 3.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆