2
我正在使用RoboGuice Providers來注入自定義依賴項。我有兩個提供者第一個提供了Facebook AccessToken,第二個提供了一個需要訪問令牌的Facebook實例,因此我使用第一個提供者注入它。RoboGuice提供錯誤
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);
}
問題是,當我運行它crashs,並顯示以下錯誤的應用程序:
java.lang.RuntimeException: Unable to start service [email protected] 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)
感謝您的答案,但由於某些原因,沒有導入@Nullable註釋。我正在使用RoboGuice 2.0,他們是否刪除了它或什麼? – user2054833
任何名爲'@ Nullable'的註解都可以使用。例如,您可以自己編寫或使用JSR-305中的一個。請參閱https://code.google.com/p/google-guice/wiki/UseNullable –