0

我正在尝试在我的应用程序中使用 RoboGuice。我的第一个尝试是将一个简单的 Fragment 更改为 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
4

2 回答 2

1

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

于 2012-06-09T12:08:20.140 回答
0

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

你的堆栈跟踪说

06-10 12:06:23.063: E/AndroidRuntime(676): Caused by: java.lang.IllegalArgumentException: MyActivity@412f8b38 似乎不是 RoboGuice 上下文(RoboContext 实例)

您的活动是 RoboFragmentActivity 吗?

于 2012-06-10T00:53:59.587 回答