java Android 无法解析构造函数意图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29350230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:05:48  来源:igfitidea点击:

Android cannot resolve constructor intent

javaandroidandroid-intentconstructorcompiler-errors

提问by Jonathan Chappell

Here is a section of my code. I am trying to make a navigation menu in which when you click on the first list item it launches the activity MrsClubb. However when I put this into my code it comes up with the error:

这是我的代码的一部分。我正在尝试制作一个导航菜单,当您单击第一个列表项时,它会启动活动MrsClubb。然而,当我把它放到我的代码中时,它出现了错误:

Cannot resolve constructor 'Intent(android.widget.AdapterView.OnItemClickListener,java.lang.Class<com....etc>)'

Any ideas how to resolve this?

任何想法如何解决这个问题?

The double ** shows where in the code the error is.

双 ** 显示错误在代码中的位置。

Here is the section of the code:

这是代码的一部分:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
    mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer);
    mDrawerList = (ListView)findViewById(android.R.id.list);
    mDrawerListItems = getResources().getStringArray(R.array.drawer_list);
    mDrawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mDrawerListItems));
    mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            switch(position) {
                case 0:
                    Intent i = new Intent**(this, MrsClubb.class);**
                    startActivity(i);
            }
            mDrawerLayout.closeDrawer(mDrawerList);

        }
    });
    mDrawerToggle = new ActionBarDrawerToggle(this,
            mDrawerLayout,
            toolbar,
            R.string.drawer_open,
            R.string.drawer_close){
        public void onDrawerClosed(View v){
            super.onDrawerClosed(v);
            invalidateOptionsMenu();
            syncState();
        }
        public void onDrawerOpened(View v){
            super.onDrawerOpened(v);
            invalidateOptionsMenu();
            syncState();
        }
    };

回答by Y.S

The Problem:

问题:

You cannot use thisto refer to the Activityinside an inner class, as thisbecomes a reference to the inner class. The meaning of the constructor not resolvedmessage is that the compiler interprets it as

您不能使用this来引用Activity内部类的内部,因为它this会成为对内部类的引用。该constructor not resolved消息的含义是编译器将其解释为

Intent(AdapterView.OnItemClickListener listener, Class class) 

which it does not recognize, instead of

它不承认,而不是

Intent(Context context, Class class)

which is correct and what the compiler expects.

这是正确的以及编译器所期望的。

The Solution:

解决方案:

Replace

代替

Intent i = new Intent(this, MrsClubb.class);

with

Intent i = new Intent(MyActivity.this, MrsClubb.class);

where MyActivityis the name of the Activityclass in which this code belongs.

哪里MyActivityActivity此代码所属的类的名称。