尝试在自定义ListView适配器中启动活动时出现AndroidRuntimeException

时间:2012-02-29 18:46:27

标签: java android listview android-arrayadapter android-activity

当我使用此自定义ListView适配器单击menuButton时,我得到的是AndroidRuntimeException:

public class MenuAdapter extends ArrayAdapter<Menu>{

    private List<Menu> menus;

    public MenuAdapter(Context context, int textViewResourceId, List<Menu> menus){
        super(context, textViewResourceId, menus);
        this.menus = menus;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View menuView = convertView;
        if (menuView == null){
            LayoutInflater menuViewInflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            menuView = menuViewInflater.inflate(R.layout.menu_row, null);
        }

        Menu menu = menus.get(position);
        if (menu != null) {
            Button menuButton = (Button)menuView.findViewById(R.id.menu_name);
            menuButton.setText(menu.name);
            menuButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent menuIntent = new Intent(v.getContext(), MenuActivity.class);
                    v.getContext().startActivity(menuIntent);
                }
            });
        }
        return menuView;
    }
}

任何想法我做错了什么?

1 个答案:

答案 0 :(得分:0)

不确定你尝试用v.getContext()做什么。尝试类似下面的内容

    final Intent intent = new Intent(this, MenuActivity.class);
    startActivityForResult(intent);

startActivity(new Intent("Your.Package.name.classnameinManifest"));