[英]Error using onClickListener (Intent)
Well, I'm trying to create an intent on a "login.java" the code is : 好吧,我正在尝试创建一个“login.java”的意图代码是:
Button btEntrar = (Button) findViewById(R.id.btnSingIn);
btEntrar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i;
i = new Intent(this, MainActivity.class);
startActivity(i);
}
});
But it says that I can't go to the other activity saying this : 但它说我不能去其他活动这样说:
Error:(24, 21) error: no suitable constructor found for Intent(,Class) constructor Intent.Intent(String,Uri) is not applicable (argument mismatch; cannot be converted to String) constructor Intent.Intent(Context,Class) is not applicable (argument mismatch; cannot be converted to Context)
错误:(24,21)错误:没有为Intent(,Class)构造函数找到合适的构造函数Intent.Intent(String,Uri)不适用(参数不匹配;无法转换为String)构造函数Intent.Intent(Context,Class)不适用(参数不匹配;无法转换为上下文)
and... 和...
Error:Execution failed for task ':app:compileDebugJava'.
错误:任务':app:compileDebugJava'的执行失败。 Compilation failed;
编译失败; see the compiler error output for details.
请参阅编译器错误输出以获取详细信
Just a few lines to explain the reason why this
does not work in: 只需几行来解释为什么
this
不起作用的原因:
i = new Intent(this, MainActivity.class)
The intent is created inside another class, here an anonymous inner class OnClickListener
. 意图是在另一个类中创建的,这里是一个匿名内部类
OnClickListener
。 Thus this
does not refer the instance of your Activity (or Context) as intended but the instance of your anonymous inner class OnClickListener
. 因此,
this
并未按预期引用您的Activity(或Context)实例,而是引用匿名内部类OnClickListener
的实例。
So you should provide the correct context of your class. 所以你应该提供你班级的正确背景。
i = new Intent(YourClassName.this, MainActivity.class)
use if you want to send it from login.java
to mainactivity.class
use 如果要将其从
login.java
发送到mainactivity.class
使用, mainactivity.class
使用
Intent intent=new Intent(login.this,Mainactivity.class);
startActivity(intent);
updated code in to your activity 更新了您活动的代码
Button btEntrar = (Button) findViewById(R.id.btnSingIn);
btEntrar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i;
i = new Intent(login.this, MainActivity.class);
startActivity(i);
}
});
try using 尝试使用
Intent i = new Intent(login.this, mainActivity.class);
hope this helps 希望这可以帮助
您是否添加了manifest.xml或尝试此代码?
Intent i = new Intent(login.this,mainActivity.class);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.