-1

have a simple Activity with a onClick method - it works but Android Studio marks startActivity red says cannot resolve method - so why?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.lyt_start);
}

public void onClick(View v) {
    switch (v.getId()) {

        case R.id.btn1:

            Intent int_Update = new Intent(v.getContext(), aty_Update.class);

            startActivity(int_Update);

            break;

        case R.id.btn2:

            Intent int_Start = new Intent(v.getContext(), aty_Main.class);
            startActivity(int_Start);

            break;
    }
}
Chad White
  • 309
  • 1
  • 4
  • 15
  • Just to make sure, this class `extends Activity`? – codeMagic Jul 08 '16 at 18:57
  • if startActivity is underlined red that means you project isn't configured correctly or building correctly. nothing is wrong with the code. Try to do a gradle sync, and i bet it will show you some errors or something. – Tomer Shemesh Jul 08 '16 at 18:58
  • Hey you are my man - you're right - after doing it ... all errors are gone - it's magic - no ist Shemesh ;)) – Chad White Jul 08 '16 at 19:28

2 Answers2

0

Don't use v.getContext(), use:

Intent int_Update = new Intent(this, aty_Update.class);

Or MyActivity.this instead.

Also, you're not setting a onClickListener correctly. Take a look at this link to learn how to do it properly.

Community
  • 1
  • 1
Vucko
  • 7,371
  • 2
  • 27
  • 45
0

Use the following code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.lyt_start);
}

public void onClick(View v) {
    switch (v.getId()) {

        case R.id.btn1:

            activityStarter(aty_Update.class);

            break;

        case R.id.btn2:

            activityStarter(aty_Main.class);

            break;
}


       Intent int_Update;

public void activityStarter(Class<?> cls){
        int_Update = new Intent(getApplicationContext(), cls);
        startActivity(int_Update);
    }
Saini
  • 734
  • 3
  • 8