Roboguice/Guice ComputationException when injector first initialized in background thread
The exception does not occur when the injector is created in the UI thread. I'm using RoboSplashActivity.
Here's the splash activity class:
package com.example.view;
import roboguice.activity.RoboSplashActivity;
import roboguice.application.RoboApplication;
import roboguice.inject.ContextScope;
import com.aa.jetaway.R;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.os.Handler;
/*snipped some imports here that are identifiable */
public class SplashActivity extends RoboSplashActivity {
@Override public void onCreate(Bundle bundle) {
setContentView(R.layout.splash);
/*
When the following two lines are uncommented, everything works, except that all the long-delayed stuff is happening in the UI thread -- not in the background w/a pretty splash page
*/
// RoboApplication app = (RoboApplication) getApplication();
// ContextScope scope = app.getInjector().getInstance(ContextScope.class);
super.onCreate(bundle);
}
@Override protected void doStuffInBackground(RoboApplication app) {
CachedData.onAppLaunch(app);
WebServiceClient.onAppLaunch(app);
}
public void startNextActivity() {
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
finish();
}
}
When the lines are commented, I get this error:
08-31 13: 17:30.671: ERROR/AndroidRuntime(10566): com.google.inject.internal.ComputationException: java.lang.ExceptionInInitializerError
08-31 13:17:30.671: ERROR/AndroidRuntime(10566): at com.google.inject.internal.MapMaker$StrategyImpl.compute(MapMaker.java:553)
08-31 13:17:30.671: ERROR/AndroidRuntime(10566): at com.google.inject.internal.MapMaker$StrategyImpl.compute(MapMaker.java:419)
08-31 13:17:30.671: ERROR/AndroidRuntime(10566): at com.google.inject.internal.CustomConcurrentHashMap$ComputingImpl.get(CustomConcurrentHashMap.java:2041)
08-31 13:17:30.671: ERROR/AndroidRuntime(10566): at com.google.inject.internal.FailableCache.get(FailableCache.java:46)
08-31 13:17:30.671: ERROR/AndroidRuntime(10566): at com.google.inject.ConstructorInjectorStore.get(ConstructorInjectorStore.java:52)
08-31 13:17:30.671: ERROR/AndroidRuntime(10566): at com.google.inject.ConstructorBindingImpl.initialize(ConstructorBindingImpl.java:57)
08-31 13:17:30.671: ERROR/AndroidRuntime(10566): at com.google.inject.InjectorImpl.initializeBinding(InjectorImpl.java:377)
08-31 13:17:30.671: ERROR/AndroidRuntime(10566): at com.google.inject.BindingProcessor$1$1.run(BindingProcessor.java:169)
08-31 13:17:30.671: ERROR/AndroidRuntime(10566): at com.google.inject.BindingProcessor.initializeBindings(BindingProcessor.java:224)
08-31 13:17:30.671: ERROR/AndroidRuntime(10566): at com.google.inject.InjectorBuilder.initializeStatically(InjectorBuilder.java:120)
08-31 13:17:30.671: ERROR/AndroidRuntime(10566): at com.google.inject.InjectorBuilder.build(InjectorBuilder.java:105)
08-31 13:17:30.671: ERROR/AndroidRuntime(10566): at com.google.inject.Guice.createInjector(Guice.java:92)
08-31 13:17:30.671: ERROR/AndroidRuntime(10566): at roboguice.application.RoboApplication.createInjector(RoboApplication.java:146)
08-31 13:17:30.671: ERROR/AndroidRuntime(10566): at roboguice.application.RoboApplication.getInjector(RoboApplication.java:84)
08-31 13:17:30.671: ERROR/AndroidRuntime(10566): at roboguice.activity.RoboSplashActivity$1.run(RoboSplashActivity.java:38)
08-31 13:17:30.671: ERROR/AndroidRuntime(10566): at java.lang.Thread.run(Thread.java:1102)
08-31 13:17:30.671: ERROR/AndroidRuntime(10566): Caused by: java.lang.ExceptionInInitializerError
08-31 13:17:30.671: ERROR/AndroidRuntime(10566): at java.lang.Class.getDeclaredConstructors(Native Method)
08-31 13:17:30.671: ERROR/AndroidRuntime(10566): at java.lang.Class.getDeclaredConstructors(Class.java:615)
08-31 13:17:30.671: ERROR/AndroidRuntime(10566): at com.google.inject.spi.InjectionPoint.forConstructorOf(InjectionPoint.java:185)
08-31 13:17:30.671: ERROR/AndroidRuntime(10566): at com.google.inject.ConstructorInjectorStore.createConstructor(ConstructorInjectorStore.java:61)
08-31 13:17:30.671: ERROR/AndroidRuntime(10566): at com.google.inject.ConstructorInjectorStore.access$000(ConstructorInjectorStore.java:31)
08-31 13:17:30.671: ERROR/AndroidRuntime(10566): at com.google.inject.ConstructorInjectorStore$1.create(ConstructorInjectorStore.java:39)
08-31 13:1开发者_运维技巧7:30.671: ERROR/AndroidRuntime(10566): at com.google.inject.ConstructorInjectorStore$1.create(ConstructorInjectorStore.java:35)
08-31 13:17:30.671: ERROR/AndroidRuntime(10566): at com.google.inject.internal.FailableCache$1.apply(FailableCache.java:35)
08-31 13:17:30.671: ERROR/AndroidRuntime(10566): at com.google.inject.internal.MapMaker$StrategyImpl.compute(MapMaker.java:549)
08-31 13:17:30.671: ERROR/AndroidRuntime(10566): ... 15 more
08-31 13:17:30.671: ERROR/AndroidRuntime(10566): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
08-31 13:17:30.671: ERROR/AndroidRuntime(10566): at android.os.Handler.<init>(Handler.java:121)
08-31 13:17:30.671: ERROR/AndroidRuntime(10566): at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421)
08-31 13:17:30.671: ERROR/AndroidRuntime(10566): at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421)
08-31 13:17:30.671: ERROR/AndroidRuntime(10566): at android.os.AsyncTask.<clinit>(AsyncTask.java:152)
08-31 13:17:30.671: ERROR/AndroidRuntime(10566): ... 24 more
Anyone have any suggestions?
This was the result of my trying to inject UI elements. Everything worked great, until I tried to create the injector in the background thread.
Answer: Don't inject ProgressDialogs, etc.
精彩评论