ListActivity是否扩展了Activity?

时间:2013-07-09 08:52:27

标签: java android slidingmenu

我正在使用滑动菜单开发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

但如果我为MainActivity.class更改了SelectFriendsActivity.class(扩展了Activity)它可以

发生了什么事?

1 个答案:

答案 0 :(得分:5)

  

为什么[我]有这些错误?

因为Activity.class不等于SelectFriendsActivity.class

您应该声明List<Class<? extends Activity>>而不是ArrayList<Class<Activity>>

见证这段代码:

    final List<Class<? extends Number>> list
        = new ArrayList<Class<? extends Number>>();

    // Both Integer and BigDecimal extend Number
    list.add(Integer.class);
    list.add(BigDecimal.class);

同样,在MenuItem中,你的getter / setter / instance成员应该返回/作为参数/具有Class<? extends Activity>类型。建议:删除setter,直接构建MenuItem,这样你就可以使它不可变。目前,由于这些爆破的setter,您的MenuItem线程安全。 Javabeans一般不是线程安全的。

  

当用户选择菜单项时,是否有更好的方法来了解我必须启动哪些活动?

不确定,因为您没有显示足够的代码。为什么你为初学者返回一个类而不是一个完全构造的实例?