Home > front end >  'MainActivity()' cannot be applied to '(android.content.Intent)'
'MainActivity()' cannot be applied to '(android.content.Intent)'

Time:04-16

How can solve this problem

See below my code

public class MainActivity extends AppCompatActivity {

private ImageView iconImage;
private LinearLayout linearLayout;
private Button register;
private Button login;



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

    iconImage = findViewById(R.id.icon_image);
    linearLayout = findViewById(R.id.linear_layout);
    register = findViewById(R.id.register);
    login = findViewById(R.id.login);

    linearLayout.animate().alpha(0f).setDuration(1);

    TranslateAnimation animation = new TranslateAnimation(0,0,0,-1000);
    animation.setDuration(1000);
    animation.setFillAfter(false);
    animation.setAnimationListener(new myAnimationListener());

     iconImage.setAnimation(animation);

     register.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View view) {
             new MainActivity(new Intent(MainActivity.this, RegisterActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP));  **Here saying that is create constructor**
         }
     });

     login.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View view) {
             new MainActivity(new Intent(MainActivity.this, LoginActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP));**Here saying that is create constructor**
         }
     });
}
private class myAnimationListener implements Animation.AnimationListener{
    @Override
    public void onAnimationStart(Animation animation) {
    }

    @Override
    public void onAnimationEnd(Animation animation) {
         iconImage.clearAnimation();
         iconImage.setVisibility(View.INVISIBLE);
         linearLayout.animate().alpha(1f).setDuration(1000);
    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }
}

}

if I will not use new operator before MainActivity(new Intent(MainActivity.this, LoginActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP)); it shows error is that Method call expected

Who's know answer of problem please answer as soon as possible

??

CodePudding user response:

Replace:

new MainActivity(new Intent(MainActivity.this, RegisterActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP));

with:

startActivity(new Intent(MainActivity.this, RegisterActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP));

(and similarly for the second new MainActivity() line)

Note that the use of startActivity() is covered in most books and introductory courses on Android app development. FWIW, here is a free book of mine, and here is the chapter in that book talking about multiple activities and startActivity().

CodePudding user response:

To start an activity, you need the startActivity() method which needs a intent as its param. But, what you do is that you are trying to open it using the class's constructor. This won't work. Try this:

startActivity(new Intent(MainActivity.this, RegisterActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP));

This code will replace

new MainActivity(new Intent(MainActivity.this, RegisterActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP));

Happy coding

  • Related