-1
public void changeActivity(Context context){
        Intent intent =new Intent(this,context.getClass());
        startActivity(intent);
        finish();
}
@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.register:
               changeActivity(RegisterActivity);
               break;
    }
}

I am using a 'changeActivity' method but I got error in line 10. The error pointed that 'Expression Expected'

Shing Ysc
  • 15
  • 6
  • change `context.getClass` this to method call `context.getClass()` like this. – Jeel Vankhede Jan 03 '19 at 14:30
  • sorry , it is my mistake of brackets but i got the error in line 10 of 'changeActivity(RegisterActivity);' – Shing Ysc Jan 03 '19 at 14:35
  • Possible duplicate of [Android: How do you go to another activity on click?](https://stackoverflow.com/questions/5163481/android-how-do-you-go-to-another-activity-on-click) – nima moradi Jan 03 '19 at 14:37

3 Answers3

0

You have pass an class object of your desination activity in your changeActivity method like this:

public void changeActivity(Class<? extends Activity> desinationActivity) { 
    Intent intent = new Intent(this, destinationActivity);
    startActivity(intent);
    finish();
}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.register:
               changeActivity(RegisterActivity.class); 
               break;
    }
}

You can check out this link for reference. Hope this helps.

Imtiaz Abir
  • 731
  • 7
  • 12
0

You're not passing your class file as object to Intent when you start activity.

Find out below solution :

public void changeActivity(Class<? extends Activity> context) { // Receive it here and provide to your intent.
    Intent intent =new Intent(this, context);
    startActivity(intent);
    finish();
}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.register:
               changeActivity(RegisterActivity.class); // pass object of your desired activity as class parameter here
               break;
    }
}
Jeel Vankhede
  • 11,592
  • 2
  • 28
  • 58
0

For writing this type of code you can check system source code. Following code of Intent constructor:

public Intent(Context packageContext, Class<?> cls) {
    mComponent = new ComponentName(packageContext, cls);
}