我正在使用RoboGuice提供程序注入自定义依赖项。我有两个提供者,第一个提供Facebook AccessToken,第二个提供需要访问令牌的Facebook实例,因此我使用第一个提供者注入它。
public class FacebookAccessTokenProvider implements Provider<AccessToken>{
@Inject FBAccountDao dao;
public AccessToken get()
{
List<FacebookAccount> accounts = null;
dao.open();
accounts = dao.findAll();
dao.close();
if (accounts != null && !accounts.isEmpty())
{
FacebookAccount account = accounts.get(0);
return new AccessToken(account.getToken(), account.getExpires());
}
return null;
}
}
public class FacebookProvider implements Provider<Facebook>{
@Inject private AccessToken accessToken;
public Facebook get()
{
if (accessToken != null)
{
Facebook facebook = new FacebookFactory().getInstance();
facebook.setOAuthAppId(AppCredentials.FACEBOOK_APP_ID,AppCredentials.FACEBOOK_APP_SECRET);
facebook.setOAuthPermissions(AppCredentials.FACEBOOK_PERMISSION);
facebook.setOAuthAccessToken(accessToken);
return facebook;
}
return null;
}
}
模块:
public void configure(Binder binder)
{
binder.bind(FBAccountDao.class).toProvider(FBAccountDaoProvider.class);
binder.bind(TwitterAccountDao.class).toProvider(TwitterAccountDaoProvider.class);
binder.bind(TwitterFriendDao.class).toProvider(TwitterFriendDaoProvider.class);
binder.bind(FeedDao.class).toProvider(FeedDaoProvider.class);
binder.bind(Facebook.class).toProvider(FacebookProvider.class);
binder.bind(Token.class).toProvider(TwitterAccessTokenProvider.class);
binder.bind(Twitter.class).toProvider(TwitterProvider.class);
binder.bind(AccessToken.class).toProvider(FacebookAccessTokenProvider.class);
}
问题是当我运行应用程序时它崩溃并显示以下错误:
java.lang.RuntimeException: Unable to start service com.soulfeed.service.FeedService@422368d8 with Intent { cmp=com.soulfeed/.service.FeedService (has extras) }: com.google.inject.ProvisionException: Guice provision errors:
1) null returned by binding at com.soulfeed.module.RoboGuiceModule.configure(RoboGuiceModule.java:34)
but com.soulfeed.module.FacebookProvider.accessToken is not @Nullable
while locating com.soulfeed.module.FacebookAccessTokenProvider
while locating facebook4j.auth.AccessToken
for field at com.soulfeed.module.FacebookProvider.accessToken(Unknown Source)
while locating com.soulfeed.module.FacebookProvider
while locating facebook4j.Facebook
1 error
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2553)
at android.app.ActivityThread.access$2000(ActivityThread.java:134)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4867)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
at dalvik.system.NativeStart.main(Native Method)
Caused by: com.google.inject.ProvisionException: Guice provision errors:
1) null returned by binding at com.soulfeed.module.RoboGuiceModule.configure(RoboGuiceModule.java:34)
but com.soulfeed.module.FacebookProvider.accessToken is not @Nullable
while locating com.soulfeed.module.FacebookAccessTokenProvider
while locating facebook4j.auth.AccessToken
for field at com.soulfeed.module.FacebookProvider.accessToken(Unknown Source)
while locating com.soulfeed.module.FacebookProvider
while locating facebook4j.Facebook
1 error
at com.google.inject.internal.InjectorImpl$4.get(InjectorImpl.java:987)
at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1013)
at roboguice.inject.ContextScopedRoboInjector.getInstance(ContextScopedRoboInjector.java:143)
at com.soulfeed.request.AsyncRequestTask.<init>(AsyncRequestTask.java:64)
at com.soulfeed.request.FeedAsyncTask.<init>(FeedAsyncTask.java:33)
at com.soulfeed.service.FeedService.onStartCommand(FeedService.java:57)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2536)
... 10 more
com.google.inject.ProvisionException: Guice provision errors:
1) null returned by binding at com.soulfeed.module.RoboGuiceModule.configure(RoboGuiceModule.java:34)
but com.soulfeed.module.FacebookProvider.accessToken is not @Nullable
while locating com.soulfeed.module.FacebookAccessTokenProvider
while locating facebook4j.auth.AccessToken
for field at com.soulfeed.module.FacebookProvider.accessToken(Unknown Source)
while locating com.soulfeed.module.FacebookProvider
while locating facebook4j.Facebook
1 error
at com.google.inject.internal.InjectorImpl$4.get(InjectorImpl.java:987)
at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1013)
at roboguice.inject.ContextScopedRoboInjector.getInstance(ContextScopedRoboInjector.java:143)
at com.soulfeed.request.AsyncRequestTask.<init>(AsyncRequestTask.java:64)
at com.soulfeed.request.FeedAsyncTask.<init>(FeedAsyncTask.java:33)
at com.soulfeed.service.FeedService.onStartCommand(FeedService.java:57)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2536)
at android.app.ActivityThread.access$2000(ActivityThread.java:134)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4867)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:3)
一般来说,从null
返回Provider
是不好的做法。 get()
的JavaDoc甚至表示&#34;必须永远不会返回null。&#34;但是,无论如何它确实有效,但您需要使用FacebookProvider.accessToken
为Guice注释@Nullable
以允许注入null
。