将片段更改为RoboGuice片段?

时间:2012-06-06 20:13:24

标签: java android roboguice

我正在尝试在我的应用中使用RoboGuice。我的第一个尝试是将一个简单的片段更改为RoboFragment。 但是应用程序无法再编译之后。这可能是什么问题?

class MyFragment extends RoboFragment {

        @InjectView(R.id.progress_bar) private static ProgressBar progressBar;
    }

结果:

    E/AndroidRuntime(1237): java.lang.RuntimeException: Unable to start activity 
ComponentInfo{path.to.MyApp}: android.view.InflateException: Binary XML file line #34: 
Error inflating class fragment
.

/编辑:

我认为我的问题是在进度条片段中我正在这样做:

public static void setProgressBar(Activity parent, int visible) {
    progressBar = (ProgressBar) parent.findViewById(R.id.progress_bar);
}

这可能不适用于RoboGuice注射?

06-10 12:06:23.063: E/AndroidRuntime(676): FATAL EXCEPTION: main
06-10 12:06:23.063: E/AndroidRuntime(676): java.lang.RuntimeException: Unable to start activity ComponentInfo{MyActivity}: android.view.InflateException: Binary XML file line #42: Error inflating class fragment
06-10 12:06:23.063: E/AndroidRuntime(676):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
06-10 12:06:23.063: E/AndroidRuntime(676):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-10 12:06:23.063: E/AndroidRuntime(676):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-10 12:06:23.063: E/AndroidRuntime(676):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-10 12:06:23.063: E/AndroidRuntime(676):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-10 12:06:23.063: E/AndroidRuntime(676):  at android.os.Looper.loop(Looper.java:137)
06-10 12:06:23.063: E/AndroidRuntime(676):  at android.app.ActivityThread.main(ActivityThread.java:4424)
06-10 12:06:23.063: E/AndroidRuntime(676):  at java.lang.reflect.Method.invokeNative(Native Method)
06-10 12:06:23.063: E/AndroidRuntime(676):  at java.lang.reflect.Method.invoke(Method.java:511)
06-10 12:06:23.063: E/AndroidRuntime(676):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-10 12:06:23.063: E/AndroidRuntime(676):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-10 12:06:23.063: E/AndroidRuntime(676):  at dalvik.system.NativeStart.main(Native Method)
06-10 12:06:23.063: E/AndroidRuntime(676): Caused by: android.view.InflateException: Binary XML file line #42: Error inflating class fragment
06-10 12:06:23.063: E/AndroidRuntime(676):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
06-10 12:06:23.063: E/AndroidRuntime(676):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
06-10 12:06:23.063: E/AndroidRuntime(676):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
06-10 12:06:23.063: E/AndroidRuntime(676):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
06-10 12:06:23.063: E/AndroidRuntime(676):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
06-10 12:06:23.063: E/AndroidRuntime(676):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
06-10 12:06:23.063: E/AndroidRuntime(676):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
06-10 12:06:23.063: E/AndroidRuntime(676):  at com.actionbarsherlock.internal.ActionBarSherlockNative.setContentView(ActionBarSherlockNative.java:119)
06-10 12:06:23.063: E/AndroidRuntime(676):  at com.actionbarsherlock.app.SherlockFragmentActivity.setContentView(SherlockFragmentActivity.java:251)
06-10 12:06:23.063: E/AndroidRuntime(676):  at MyActivity.onCreate(MyActivity.java:109)
06-10 12:06:23.063: E/AndroidRuntime(676):  at android.app.Activity.performCreate(Activity.java:4465)
06-10 12:06:23.063: E/AndroidRuntime(676):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-10 12:06:23.063: E/AndroidRuntime(676):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
06-10 12:06:23.063: E/AndroidRuntime(676):  ... 11 more
06-10 12:06:23.063: E/AndroidRuntime(676): Caused by: java.lang.IllegalArgumentException: MyActivity@412f8b38 does not appear to be a RoboGuice context (instanceof RoboContext)
06-10 12:06:23.063: E/AndroidRuntime(676):  at roboguice.inject.ContextScope.getScopedObjectMap(ContextScope.java:147)
06-10 12:06:23.063: E/AndroidRuntime(676):  at roboguice.inject.ContextScope.enter(ContextScope.java:79)
06-10 12:06:23.063: E/AndroidRuntime(676):  at roboguice.inject.ContextScopedRoboInjector.injectMembersWithoutViews(ContextScopedRoboInjector.java:241)
06-10 12:06:23.063: E/AndroidRuntime(676):  at roboguice.fragment.RoboFragment.onCreate(RoboFragment.java:13)
06-10 12:06:23.063: E/AndroidRuntime(676):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:834)
06-10 12:06:23.063: E/AndroidRuntime(676):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1058)
06-10 12:06:23.063: E/AndroidRuntime(676):  at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1156)
06-10 12:06:23.063: E/AndroidRuntime(676):  at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:270)
06-10 12:06:23.063: E/AndroidRuntime(676):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
06-10 12:06:23.063: E/AndroidRuntime(676):  ... 23 more

2 个答案:

答案 0 :(得分:1)

为什么“progressBar”字段是静态的?

答案 1 :(得分:0)

如果您发布包含所有异常的完整logcat输出,我们可以尝试找出您应该修复的那个。 xml inflate行34中包含.fragment,logcat输出中的片段应该显示异常,告诉你片段中的哪一行包含错误。

您的堆栈跟踪说

  

06-10 12:06:23.063:E / AndroidRuntime(676):引起:   java.lang.IllegalArgumentException:MyActivity @ 412f8b38没有   似乎是RoboGuice上下文(instanceof RoboContext)

您的活动是否为RoboFragmentActivity?