当注入器首先在后台线程中初始化时,Roboguice / Guice ComputationException

时间:2011-08-31 18:28:30

标签: android guice roboguice

在UI线程中创建注入器时不会发生异常。我正在使用RoboSplashActivity。

这是启动活动类:

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

}

当评论这些行时,我收到此错误:

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:17: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

有人有任何建议吗?

1 个答案:

答案 0 :(得分:0)

这是我尝试注入UI元素的结果。一切都很顺利,直到我尝试在后台线程中创建注入器。

答案:不要注入ProgressDialogs等。