cannot resolve context after importing android.content.Context; also

15,379

Solution 1

context is not declared by you, so I suggest you to use following lines of code:

Intent intent = new Intent(MainActivity.this, LogIn.class);//MainActivity.this contains your current context
startActivity(intent);

Still any doubt then feel free to ask.

Solution 2

context is not declared and initialized

Change this

Intent intent = new Intent(context, LogIn.class);

to

Intent intent = new Intent(MainActivity.this, LogIn.class);

Similarly for the other intent

Your MainActivity extends Activity

java.lang.Object
   ↳    android.content.Context
       ↳    android.content.ContextWrapper
           ↳    android.view.ContextThemeWrapper
               ↳    android.app.Activity

So to get context you can have MainActivity.this.

Also i don't see where you initiailze your views and your Activity does not implement OnClickListener interface

Share:
15,379
richapathak
Author by

richapathak

Updated on June 14, 2022

Comments

  • richapathak
    richapathak almost 2 years

    This is my mainactivity.java class

    package com.example.apurva.therisingsatyam;
    
    import android.os.Bundle;
    import android.content.Context;
    import android.support.v7.app.ActionBarActivity;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.content.Intent;
    
    public class MainActivity extends ActionBarActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
    
            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    
        public void onClick(View v) {
    
            switch (v.getId()) {
                case R.id.button:
                    Intent intent = new Intent(context, LogIn.class);
                    startActivity(intent);
                    break;
                case R.id.button2:
                    Intent intent1 = new Intent(context, SignUp.class);
                    startActivity(intent1);
                    break;
            }
        }
    }
    

    I am trying to switch activity when the button is clicked.But as can be seen in above code there is a red line under context word at both the places in onClick method, although I have imported android.content.context . Please someone help me out to solve this issue.

  • richapathak
    richapathak about 9 years
    I am totally new to android. I am trying to make two buttons pointing to two different login and signup activity. I got this code online.Is there any problem in this code and plz tell me what do you mean by....."Also i don't see where you initialze your views and your Activity does not implement OnClickListener interface "
  • Raghunandan
    Raghunandan about 9 years