如何正确使用Android的支持库 [英] How to use android support library correctly

查看:190
本文介绍了如何正确使用Android的支持库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过专业的Andr​​oid 4.0应用程序开发工作我的方式。第4章修改待办事项应用程序使用的片段,但我想测试一个姜饼设备上。有提及使用支持库,允许使用较低版本的设备上的Andr​​oid V3或V4特征的书,但它不是盖得非常好。

I'm working my way through Professional Android 4 Application Development. Chapter 4 modifies the To Do List app to use fragments, but I'm trying to test on a Gingerbread device. There's mention in the book of using support libraries to allow using Android v3 or v4 features on a lower version device, but it's not covered very well.

我遇到一个问题,特别是与:

I'm running into a problem specifically with:

    // Get references to the Fragments
    android.app.FragmentManager fm = getFragmentManager();
    ToDoListFragment    todoListFragment = (ToDoListFragment) fm.findFragmentById( R.id.ToDoListFragment );

我有这些进口在顶部:   进口android.support.v4.app.FragmentManager;   进口android.support.v4.app.ListFragment;

I've got these imports at the top: import android.support.v4.app.FragmentManager; import android.support.v4.app.ListFragment;

但皮棉警告说,对ToDoListFragment todoListFragment =(ToDoListFragment)行:   不能投从片段ToDoListFragment

But lint warns on the "ToDoListFragment todoListFragment = (ToDoListFragment)" line: cannot cast from Fragment to ToDoListFragment

在我ToDoListFragment课,我有:

In my ToDoListFragment class, I have:

    import android.support.v4.app.ListFragment;

    public class ToDoListFragment extends ListFragment {
    }

这是从书上几乎一字不差,除了改变使用支持库。

This is almost verbatim from the book, except for the change to use support library.

我不太清楚如何让这个code使用V4支持库正常运行。我提前道歉,如果这是不足够的信息。我仍然在学习这一点,我更熟悉C / C ++比Java。如果我不使用的支持库中,code构建就好了,并运行冰淇淋三明治设备上,但我希望得到它的工作在较低的水平的设备了。

I'm not clear on how to get this code to work correctly using the v4 support library. I apologize in advance if this isn't enough info. I'm still learning this, and I'm more familiar with C/C++ than Java. If I don't use the support library, the code builds just fine and will run on an Ice Cream Sandwich device, but I'd like to get it working on lower level devices, too.

推荐答案

您应该使用 getSupportFragmentManager()而不是 getFragmentManager()

android.support.v4.app.FragmentManager fm = getSupportFragmentManager()

这篇关于如何正确使用Android的支持库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆