Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android cannot resolve constructor intent

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:

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();
        }
    };
like image 954
Jonathan Chappell Avatar asked Mar 30 '15 15:03

Jonathan Chappell


1 Answers

The Problem:

You cannot use this to refer to the Activity inside an inner class, as this becomes a reference to the inner class. The meaning of the constructor not resolved message is that the compiler interprets it as

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 MyActivity is the name of the Activity class in which this code belongs.

like image 58
Y.S Avatar answered Sep 17 '22 17:09

Y.S