本文介绍了DrawerItemClickListener不能被解析为一个类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在建立DrawerItemClickListener但是我得到一个错误,说明的过程:即使我宣布该方法在code进一步向下DrawerItemClickListener不能被解析为一个类型。我看了过来好几次,我不明白我做错了。

 私人无效_initMenu(){        drawerListView.setOnItemClickListener(新DrawerItemClickListener());    }    处理器ResponseHandler所=新的处理程序(){
        @覆盖
        公共无效的handleMessage(消息MSG){
            populateListWithVideos(MSG);
        };
    };    私人无效populateListWithVideos(消息MSG){
        库lib =(库)msg.getData()得到(
                GetYouTubeUserVideosTask.LIBRARY);
        listView.setVideos(lib.getVideos());
    }    @覆盖
    保护无效的onStop(){
        ResponseHandler所= NULL;
        super.onStop();
    }    @覆盖
    公共无效onVideoClicked(视频短片){
        意向意图=新意图(这一点,Player.class);
        intent.putExtra(ID,video.getId());
        intent.putExtra(称号,video.getTitle());
        intent.putExtra(上载,video.getUploader());
        intent.putExtra(观看次数,video.getviewCount());
        startActivity(意向);
    }    @覆盖
    公共无效onConfigurationChanged(配置NEWCONFIG){
        super.onConfigurationChanged(NEWCONFIG);
        actionBarDrawerToggle.onConfigurationChanged(NEWCONFIG);
    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        MenuInflater吹气= getMenuInflater();
        inflater.inflate(R.menu.main,菜单);
        返回super.onCreateOptionsMenu(菜单);
    }    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){        如果(actionBarDrawerToggle.onOptionsItemSelected(项目)){
            返回true;
        }
        返回super.onOptionsItemSelected(项目);
    }    私有类ImagePagerAdapter扩展PagerAdapter {
        公共ImagePagerAdapter(活动行为,INT [] mImages,
                的String [] stringArra){
            imageArray = mImages;
            活性=行为;
            字符串数组= stringArra;
        }        //这是你的构造
        公共ImagePagerAdapter(){
            超();        }        私人诠释[] = mImages新INT [] {R.drawable.selstation_up_btn,
                R.drawable.classical_up_btn,R.drawable.country_up_btn,
                R.drawable.dance_up_btn,R.drawable.hiphop_up_btn,
                R.drawable.island_up_btn,R.drawable.latin_up_btn,
                R.drawable.pop_up_btn,R.drawable.samba_up_btn};        私有String []字符串数组=新的String [] {VEVO
                TheMozARTGROUP,TimMcGrawVEVO,TiestoVEVO,
        EminemVEVO};        @覆盖
        公众诠释的getCount(){
            返回mImages.length;
        }        @覆盖
        公共布尔isViewFromObject(查看视图,Object对象){
            返回查看==((ImageView的)对象);
        }        @覆盖
        公共对象instantiateItem(ViewGroup中的容器,INT位置){
            上下文的背景下= Home.this;
            ImageView的ImageView的=新ImageView的(上下文);
            imageView.setScaleType(ScaleType.FIT_XY);
            imageView.setImageResource(mImages [位置]);
            ((ViewPager)容器).addView(ImageView的,0);
            返回ImageView的;
        }        公共无效destroyItem(ViewGroup中的容器,INT位置,Object对象){
            ((ViewPager)容器).removeView((ImageView的)对象);
        }        私有类DrawerItemClickListener工具
        ListView.OnItemClickListener {            @覆盖
            公共无效onItemClick(适配器视图父母,观景,
                    INT位置,长的id){                drawerListView.setItemChecked(位置,真正的);
                字符串文本=菜单中点击......应实行
                Toast.makeText(Home.this,文字,Toast.LENGTH_LONG).show();
                drawerLayout.closeDrawer(drawerListView);            }
        }
    }
}


解决方案

您需要创建类DrawerItemClickListener实现ListView.OnItemClickListener 结果结果
在Android的文档完整的例子和说明的

这是它的片段:结果

 私有类DrawerItemClickListener实现ListView.OnItemClickListener {
    @覆盖
    公共无效onItemClick(适配器视图父母,观景,INT位置,长的id){
        选择信息(位置);
    }
}/ **片段互换的主要内容视图* /
私人无效选择信息(INT位置){
    //创建一个新片段,并指定这个星球表明,基于对位置
    片段片段=新PlanetFragment();
    捆绑ARGS =新包();
    args.putInt(PlanetFragment.ARG_PLANET_NUMBER,位置);
    fragment.setArguments(参数);    //将替换现有的片段插入片段
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
                   .replace(R.id.content_frame,片段)
                   。承诺();    //高亮显示所选项目,更新称号,并关​​上抽屉
    mDrawerList.setItemChecked(位置,真正的);
    的setTitle(mPlanetTitles [位置]);
    mDrawerLayout.closeDrawer(mDrawerList);
}

I'm in the process of setting up a DrawerItemClickListener however I'm getting an error stating: DrawerItemClickListener cannot be resolved to a type even though I declare the method further down in the code. I've looked it over several times and I do not see what I have done wrong.

    private void _initMenu() {

        drawerListView.setOnItemClickListener(new DrawerItemClickListener());

    }

    Handler responseHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            populateListWithVideos(msg);
        };
    };

    private void populateListWithVideos(Message msg) {
        Library lib = (Library) msg.getData().get(
                GetYouTubeUserVideosTask.LIBRARY);
        listView.setVideos(lib.getVideos());
    }

    @Override
    protected void onStop() {
        responseHandler = null;
        super.onStop();
    }

    @Override
    public void onVideoClicked(Video video) {
        Intent intent = new Intent(this, Player.class);
        intent.putExtra("id", video.getId());
        intent.putExtra("title", video.getTitle());
        intent.putExtra("uploader", video.getUploader());
        intent.putExtra("viewCount", video.getviewCount());
        startActivity(intent);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        actionBarDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private class ImagePagerAdapter extends PagerAdapter {
        public ImagePagerAdapter(Activity act, int[] mImages,
                String[] stringArra) {
            imageArray = mImages;
            activity = act;
            stringArray = stringArra;
        }

        // this is your constructor
        public ImagePagerAdapter() {
            super();

        }

        private int[] mImages = new int[] { R.drawable.selstation_up_btn,
                R.drawable.classical_up_btn, R.drawable.country_up_btn,
                R.drawable.dance_up_btn, R.drawable.hiphop_up_btn,
                R.drawable.island_up_btn, R.drawable.latin_up_btn,
                R.drawable.pop_up_btn, R.drawable.samba_up_btn };

        private String[] stringArray = new String[] { "vevo",
                "TheMozARTGROUP‎", "TimMcGrawVEVO‎", "TiestoVEVO‎",
        "EminemVEVO‎" };

        @Override
        public int getCount() {
            return mImages.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == ((ImageView) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            Context context = Home.this;
            ImageView imageView = new ImageView(context);
            imageView.setScaleType(ScaleType.FIT_XY);
            imageView.setImageResource(mImages[position]);
            ((ViewPager) container).addView(imageView, 0);
            return imageView;
        }

        public void destroyItem(ViewGroup container, int position, Object object) {
            ((ViewPager) container).removeView((ImageView) object);
        }

        private class DrawerItemClickListener implements
        ListView.OnItemClickListener {

            @Override
            public void onItemClick(AdapterView parent, View view,
                    int position, long id) {

                drawerListView.setItemChecked(position, true);


                String text = "menu click... should be implemented";
                Toast.makeText(Home.this, text, Toast.LENGTH_LONG).show();
                drawerLayout.closeDrawer(drawerListView);

            }
        }
    }
}
解决方案

You need to create class DrawerItemClickListener implements ListView.OnItemClickListener

Full example and explanation in Android docs https://developer.android.com/training/implementing-navigation/nav-drawer.html#ListItemClicks

the fragment from it:

private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        selectItem(position);
    }
}

/** Swaps fragments in the main content view */
private void selectItem(int position) {
    // Create a new fragment and specify the planet to show based on position
    Fragment fragment = new PlanetFragment();
    Bundle args = new Bundle();
    args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
    fragment.setArguments(args);

    // Insert the fragment by replacing any existing fragment
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
                   .replace(R.id.content_frame, fragment)
                   .commit();

    // Highlight the selected item, update the title, and close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(mPlanetTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
}

这篇关于DrawerItemClickListener不能被解析为一个类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 09:02