【问题标题】:How to use android support library correctly如何正确使用android支持库
【发布时间】:2014-03-22 06:31:24
【问题描述】:

我正在通过专业的 Android 4 应用程序开发工作。第 4 章修改了 To Do List 应用程序以使用片段,但我正在尝试在 Gingerbread 设备上进行测试。书中提到了使用支持库来允许在较低版本的设备上使用 Android v3 或 v4 功能,但没有很好地涵盖。

我遇到了一个具体的问题:

    // 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;

但 lint 在“ToDoListFragment todoListFragment = (ToDoListFragment)”行发出警告: 无法从 Fragment 转换为 ToDoListFragment

在我的 ToDoListFragment 类中,我有:

    import android.support.v4.app.ListFragment;

    public class ToDoListFragment extends ListFragment {
    }

这几乎是书中的逐字记录,除了使用支持库的更改。

我不清楚如何使用 v4 支持库使此代码正常工作。如果这不是足够的信息,我提前道歉。我还在学习这个,而且我对 C/C++ 比对 Java 更熟悉。如果我不使用支持库,代码构建得很好,可以在 Ice Cream Sandwich 设备上运行,但我也想让它在较低级别的设备上运行。

【问题讨论】:

    标签: android android-support-library


    【解决方案1】:

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

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

    【讨论】:

    • 这是一个很好的答案。此外,请注意支持库中的类通常与最新的 android 版本中的类同名。所以仔细检查你的进口。这适用于:Notification.Builder、Fragment、Menu、MenuItem、FragmentManager、Loader、LoaderManager、Loader.CallBacks。您应该始终导入 android.support.vx.** 版本而不是 android.app.** 版本
    • 哦,哇,太好了!感谢你们两位这么快的回答!
    • 如果有帮助,请接受@Alex 的回答。这就是 stack over flow 的工作方式,也是奖励帮助您的志愿者的一种方式。
    • 嗯,我希望这可以解决问题,但由于某种原因,它不起作用。正如 Alex 建议的那样,我有 android.support.v4.app.FragmentManager fm = getSupportFragmentManager(),但 Eclipse 给了我“方法 getSupportFragmentManager() 对于 ToDoListActivity 类型未定义”。我想我已经正确设置了所有导入,所以我不确定我做错了什么。我想如果我不能快速完成这项工作,我将不得不先在 ICS 设备上进行测试,直到我有更多的 Android 编程经验。
    • @WarnerYoung 确保 ToDoListActivity 扩展了 android.support.v4.app.FragmentActivity
    【解决方案2】:

    我想对这个例子做同样的事情。 有几个地方需要更改才能使其与支持库一起使用。 这是我完整的 java 文件,在 cmets 中突出显示了更改:

    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();
        }           
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-02
      相关资源
      最近更新 更多