Android:兼容包Fragment崩溃

发布于 2024-11-27 13:00:13 字数 1326 浏览 1 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

跨年 2024-12-04 13:00:13

谢谢 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.

指尖凝香 2024-12-04 13:00:13

有时您不希望将 UI 附加到您的 Fragment。例如,在我的应用程序中,我有一个片段负责用作操作栏中的操作视图的菜单项。在这种情况下,您无法实现 onCreateView()

Android Fragment 用户指南中的“添加没有 UI 的片段”部分,您必须以编程方式将片段添加到他的活动中。

以下是我在活动中使用的代码:

// Add the address bar fragment
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(addressBarFragment,"address_bar_fragment");
fragmentTransaction.commit();

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

// Add the address bar fragment
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(addressBarFragment,"address_bar_fragment");
fragmentTransaction.commit();

Note 1: I use getSupportFragmentManager() instead of getFragmentManager() because I use the compatibility library.
Note 2: new Fragment() is not called in my example because I use Roboguice for dependences injection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文