Java 无法解析构造函数(Android Intent)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30965292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot resolve constructor (Android Intent)
提问by User
I'm trying to create a simple button that opens to a different activity:
我正在尝试创建一个打开不同活动的简单按钮:
package com.example.xxx.buttonexample;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnClick();
}
public void btnClick() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
Intent intent = new Intent(this,emergencyIntent.class);
startActivity(intent);
}
});
}
}
Here is my emergencyIntent.class file:
这是我的 EmergencyIntent.class 文件:
package com.example.xxx.buttonexample;
import android.app.Activity;
import android.os.Bundle;
public class emergencyIntent extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// The activity is being created.
}
}
I received a error:
我收到一个错误:
"Cannot resolve constructor 'intent(anonymous android.view.View.OnClickListener, java.lang.Class(com.example.xxx.buttonexample.emergencyIntent))'.
“无法解析构造函数'intent(匿名android.view.View.OnClickListener,java.lang.Class(com.example.xxx.buttonexample.emergencyIntent))'。
采纳答案by Krupal Shah
Just replace this
in first parameter with MainActivity.this
. like:
只需将this
第一个参数替换为MainActivity.this
. 喜欢:
Intent intent = new Intent(MainActivity.this,emergencyIntent.class);
The error is because you are writing it in public void onClick(View v)
, where 'this
' will mean instance of anonymous class that implements View.OnClickListener
. while first parameter in Intent constructor Intent(Context context, Class<?> cls)
requires Activity context.
错误是因为您正在编写它public void onClick(View v)
,其中 ' this
' 将表示实现View.OnClickListener
. 而 Intent 构造函数中的第一个参数Intent(Context context, Class<?> cls)
需要 Activity 上下文。
回答by Yogesh Borhade
package com.example.xxx.buttonexample;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this,emergencyIntent.class);
startActivity(intent);
//Or Intent intent = new Intent(getApplicationContext(),emergencyIntent.class);
}
});
}
}
回答by user6741985
package com.example.xxx.buttonexample;
包 com.example.xxx.buttonexample;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this,emergencyIntent.class);
startActivity(intent); //Or Intent intent = new Intent(getApplicationContext(),emergencyIntent.class);
开始活动(意图);//或 Intent intent = new Intent(getApplicationContext(),emergencyIntent.class);
}
});## Heading ##
});##标题##