如何正确使用Android支持库

8
我正在学习《Android应用开发实战指南》第四章,该章节修改了“待办事项清单”应用程序以使用片段,但我想在一台Gingerbread设备上进行测试。书中提到使用支持库可以允许在低版本设备上使用Android v3或v4的功能,但没有很好地介绍。
我遇到了一个具体的问题:
    // Get references to the Fragments
    android.app.FragmentManager fm = getFragmentManager();
    ToDoListFragment    todoListFragment = (ToDoListFragment) fm.findFragmentById( R.id.ToDoListFragment );

我在顶部导入了以下内容: import android.support.v4.app.FragmentManager; import android.support.v4.app.ListFragment;
但是lint在"ToDoListFragment todoListFragment = (ToDoListFragment)"这一行上发出警告: 无法将Fragment转换为ToDoListFragment
在我的ToDoListFragment类中,我有:
    import android.support.v4.app.ListFragment;

    public class ToDoListFragment extends ListFragment {
    }

这段话几乎是从书中摘抄而来,只是更改了使用支持库的方式。
我不确定如何使用v4支持库使此代码正确运行。如果这不足够信息,请提前谅解。我正在学习这个,比Java更熟悉C/C++。如果不使用支持库,该代码可以编译并在Ice Cream Sandwich设备上运行,但我希望它也能在低版本的设备上运行。
2个回答

13

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

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

4
这是一个好回答。此外,请注意,支持库中的类名称通常与最新的Android版本中的类名称相同。因此,请仔细检查您的导入内容。这适用于:Notification.Builder,Fragment,Menu,MenuItem,FragmentManager,Loader,LoaderManager和Loader.CallBacks等。您应该始终导入android.support.vx.**版本,而不是android.app.**版本。 - Snicolas
哦,哇,太棒了!感谢你们俩如此迅速的回答! - WarnerYoung
如果@Alex的回答有帮助,请接受他的答案。这是Stack Overflow的工作方式,也是奖励帮助你的志愿者的一种方式。 - Snicolas
嗯,我本以为这个会奏效,但出于某些原因,它并没有起作用。我按照Alex的建议写了android.support.v4.app.FragmentManager fm = getSupportFragmentManager(),但Eclipse给了我一个错误:“The method getSupportFragmentManager() is undefined for the type ToDoListActivity”。我认为我已经正确设置了所有导入,所以不确定我做错了什么。我想如果我不能快速解决这个问题,我将不得不先在ICS设备上进行测试,直到我对Android编程有更多的经验。 - WarnerYoung
1
@WarnerYoung 请确保ToDoListActivity继承自android.support.v4.app.FragmentActivity。 - Alexander Kulyakhtin

2

我希望通过这个例子来实现同样的效果。 有几个地方需要调整以使其能够与支持库一起工作。 以下是我进行了更改并在注释中突出显示的完整Java文件:

package com.paad.todolist;

import java.util.ArrayList;
import android.support.v4.app.FragmentActivity; // Because we're using the support library 
                                                // version of fragments, the import has to be
                                                // FragmentActivity rather than Activity
import android.support.v4.app.FragmentManager;  // Support version of Fragment Manager
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;

// because we're using the support library version of fragments, the class has to extend the
// FragmentActivity superclass rather than the more usual Activity superclass
public class ToDoListActivity extends FragmentActivity implements NewItemFragment.OnNewItemAddedListener {

    // logging tag
    private static final String TAG = "ToDoListActivity";

    // create an array adaptor ready to bind the array to the list view
    private ArrayAdapter<String> aa;

    // set up array list to hold the ToDo items
    private ArrayList<String> todoItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.i(TAG, "The onCreate method has started");

        // inflate the view
        setContentView(R.layout.activity_to_do_list);

        // get references to the fragments

        // FragmentManager fm = getFragmentManager(); this won't work with the support library version
        FragmentManager fm = getSupportFragmentManager();   // this is the equivalent for support library

        ToDoListFragment todoListFragment = (ToDoListFragment)fm.findFragmentById(R.id.ToDoListFragment);

        // Create the array list of to do items
        todoItems = new ArrayList<String>();

        // Create the array adapter to bind the array to the listview
        aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoItems);

        // bind the array adapter to the list view
        todoListFragment.setListAdapter(aa);        
    }

    // implement the listener... It adds the received string to the array list
    // then notifies the array adapter of the dataset change
    public void onNewItemAdded(String newItem) {
        todoItems.add(newItem);
        aa.notifyDataSetChanged();
    }           
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接