'MainActivity()' 不能应用于“(android.content.Intent)”
'MainActivity()' cannot be applied to '(android.content.Intent)'
我该如何解决这个问题?
我的代码:
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) {
}
}
}
如果我在 MainActivity(new Intent(MainActivity.this, LoginActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP));
之前没有使用 new 运算符,它会显示错误是预期的方法调用。如何解决此错误?
替换:
new MainActivity(new Intent(MainActivity.this, RegisterActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP));
与:
startActivity(new Intent(MainActivity.this, RegisterActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP));
(第二个 new MainActivity()
行也类似)
请注意,大多数关于 Android 应用程序开发的书籍和入门课程都介绍了 startActivity()
的使用。 FWIW,here is a free book of mine, and here is the chapter in that book talking about multiple activities and startActivity()
。
我该如何解决这个问题?
我的代码:
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) {
}
}
}
如果我在 MainActivity(new Intent(MainActivity.this, LoginActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP));
之前没有使用 new 运算符,它会显示错误是预期的方法调用。如何解决此错误?
替换:
new MainActivity(new Intent(MainActivity.this, RegisterActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP));
与:
startActivity(new Intent(MainActivity.this, RegisterActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP));
(第二个 new MainActivity()
行也类似)
请注意,大多数关于 Android 应用程序开发的书籍和入门课程都介绍了 startActivity()
的使用。 FWIW,here is a free book of mine, and here is the chapter in that book talking about multiple activities and startActivity()
。