我正在通过专业Android 4应用程序开发工作。第4章修改待办事项列表应用程序以使用片段,但我正在尝试在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支持库使此代码正常工作。如果这还不够,我会提前道歉。我还在学习这个,而且我比C / C ++更熟悉Java。如果我不使用支持库,代码构建得很好并将在Ice Cream Sandwich设备上运行,但我也希望它能够在更低级别的设备上运行。
答案 0 :(得分:13)
您应该使用getSupportFragmentManager()
代替getFragmentManager()
android.support.v4.app.FragmentManager fm = getSupportFragmentManager()
答案 1 :(得分: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();
}
}