我正在开发一个带有滑动菜单的 Android 应用程序。
我创建了一个自定义类以用于ListFragment
显示菜单项:
public class MenuItem
{
/**
* Title to show on sliding menu.
*/
private String title;
/**
* Activity to start when user select a menu item.
*/
private Class<Activity> activityClass;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Class<Activity> getActivityClass() {
return activityClass;
}
public void setActivityClass(Class<Activity> activityClass) {
this.activityClass = activityClass;
}
}
这是我的ListFragment
:
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.com.msx.R;
import com.com.msx.adapters.MenuAdapter;
import com.com.msx.menu.MenuItem;
public class MenuFragment extends ListFragment
{
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
return inflater.inflate(R.layout.menu_list, null);
}
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
ArrayList<MenuItem> menuItems = getMenuItems();
MenuAdapter adapter = new MenuAdapter(getActivity(),
R.layout.menu_row, menuItems);
setListAdapter(adapter);
}
/**
* Get menu items to show on Sliding menu.
* @return
*/
private ArrayList<MenuItem> getMenuItems()
{
String[] titles = {
getActivity().getString(R.string.menu_item_my_lovers),
getActivity().getString(R.string.menu_item_my_friends),
getActivity().getString(R.string.menu_item_geo_sex_locator),
getActivity().getString(R.string.menu_item_sex_affinity),
getActivity().getString(R.string.menu_item_messages),
getActivity().getString(R.string.menu_item_statistics)
};
// TODO: Add icons for each menu item.
ArrayList<Class<? extends Activity>> activities = new ArrayList<Class<? extends Activity>>(titles.length);
activities.add(SelectFriendsActivity);
ArrayList<MenuItem> items = new ArrayList<MenuItem>(titles.length);
for(int i = 0; i < titles.length; i++)
{
MenuItem item = new MenuItem();
item.setTitle(titles[i]);
// TODO: Set icon.
items.add(item);
}
return items;
}
}
现在,我正在尝试ListFragment
用自定义填充列表的适配器ArrayAdapter
。但是当我尝试这样做时:
ArrayList<Class<Activity>> activities = new ArrayList<Class<Activity>>(titles.length);
activities.add((Class<Activity>)SelectFriendsActivity.class);
我得到这两个编译器错误:
Cannot cast from Class<SelectFriendsActivity> to Class<Activity>
SelectFriendsActivity cannot be resolved to a type
如果我这样做:
ArrayList<Class<? extends Activity>> activities = new ArrayList<Class<? extends Activity>>(titles.length);
activities.add(SelectFriendsActivity.class);
我得到这个编译器错误:
The method add(Class<? extends Activity>) in the type ArrayList<Class<? extends Activity>>
is not applicable for the arguments (Class<SelectFriendsActivity>)
SelectFriendsActivity
有这个原型:
public class SelectFriendsActivity extends android.app.ListActivity
但是,如果我将 SelectFriendsActivity.class 更改为 MainActivity.class(它扩展了 Activity),它就可以工作了!
发生了什么?