Android:兼容包Fragment崩溃
我正在尝试使用 Android 兼容性包来创建一个使用 Fragments 的向后兼容的应用程序。但是,当我在 Android v2.2 模拟器上运行它时,它崩溃了。它在我的 Xoom (v3.2) 上不会崩溃。我怀疑 main.xml 中的片段标签可能是原因:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<fragment android:name="com.companyname.appname.MainMenuFragment"
android:id="@+id/mainMenu"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="fill_parent" />
</LinearLayout>
这是 FragmentActivity:
package com.companyname.appname;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class AppName extends FragmentActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
这是 Fragment:
package com.companyname.appname;
import android.support.v4.app.Fragment;
public class MainMenuFragment extends Fragment {
}
有什么想法吗?
谢谢
编辑:我的目标是 API 级别 8 (Android v2.2)
I am trying to use the Android compatibility package to create a backwards-compatible app that uses Fragments. However, it crashes when I run it on an Android v2.2 emulator. It does not crash on my Xoom (v3.2). I suspect the fragment tag in the main.xml might be the cause:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<fragment android:name="com.companyname.appname.MainMenuFragment"
android:id="@+id/mainMenu"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="fill_parent" />
</LinearLayout>
Here is the FragmentActivity:
package com.companyname.appname;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class AppName extends FragmentActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
And here is the Fragment:
package com.companyname.appname;
import android.support.v4.app.Fragment;
public class MainMenuFragment extends Fragment {
}
Any ideas?
Thanks
EDIT: I have targeted API level 8 (Android v2.2)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
谢谢 smith324 和 LeffelMania。错误 logcat 显示此错误:08-03 22:03:22.946: ERROR/AndroidRuntime(938): Caused by: java.lang.IllegalStateException: Fragment com.companyname.appname.MainMenuFragment 未创建视图。因此,我在 MainMenuFragment 类中覆盖了 onCreateView() 并让它返回一个 View,这有效。奇怪的是它在 v3.2 中没有崩溃。
Thanks, smith324 and LeffelMania. The error logcat showed this error: 08-03 22:03:22.946: ERROR/AndroidRuntime(938): Caused by: java.lang.IllegalStateException: Fragment com.companyname.appname.MainMenuFragment did not create a view. So I overrode onCreateView() in my MainMenuFragment class and had it return a View, and this worked. Strange that it didn't crash in v3.2.
有时您不希望将 UI 附加到您的 Fragment。例如,在我的应用程序中,我有一个片段负责用作操作栏中的操作视图的菜单项。在这种情况下,您无法实现
onCreateView()
。如 Android Fragment 用户指南中的“添加没有 UI 的片段”部分,您必须以编程方式将片段添加到他的活动中。
以下是我在活动中使用的代码:
注 1: 我使用
getSupportFragmentManager()
而不是getFragmentManager()
因为我使用兼容性库.注 2: 在我的示例中没有调用 new Fragment(),因为我使用 Roboguice 进行依赖注入。
Sometimes you doesn't want an UI to be attached to your Fragment. For example in my app I have a fragment responsible for a menu item used as an action view in the action bar. In this case you can't implement
onCreateView()
.As described in the Android Fragment user guide in the "Adding a fragment without a UI" section, you have to add the fragment to his activity programmatically.
Here is the code I use in my activity :
Note 1: I use
getSupportFragmentManager()
instead ofgetFragmentManager()
because I use the compatibility library.Note 2: new Fragment() is not called in my example because I use Roboguice for dependences injection.