我尝试通过点击图片按钮从主要活动开始新活动但我收到此错误:
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
这是我的代码:
public void WordsButton() {
words = (ImageButton) findViewById(R.id.words);
words.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(), WordsActivity.class);
getApplicationContext.startActivity(intent);
}
});
}
答案 0 :(得分:1)
尝试将onClick方法参数从arg0更改为v,使其与接口中的方法定义相同
这是一个建议
words.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), WordsActivity.class);
startActivity(intent);
}
});
答案 1 :(得分:0)
将您的getApplicationContext.startActivity(intent);
更改为startActivity(intent)
答案 2 :(得分:0)
请勿使用getApplicationContext()
只需使用标准的活动上下文getContext()
答案 3 :(得分:0)
你应该用这个:
words.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity.this, WordsActivity.class);
startActivity(intent);
}
});
答案 4 :(得分:0)
使用此:
public void WordsButton() {
words = (ImageButton) findViewById(R.id.words);
words.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(), WordsActivity.class);
startActivity(intent);
}
});
}
答案 5 :(得分:0)
将此标记添加到您的意图
MyApp
使用标准上下文getContext()
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tapGestureAction(_:)))
view.addGestureRecognizer(tapGesture)
}
func tapGestureAction(_ tapGesture: UITapGestureRecognizer) {
let tappedLocationPoint = tapGesture.location(in: view)
print(tappedLocationPoint.x)
print(tappedLocationPoint.y)
// determining which area has been tapped based on what's the value of x and y
// you logic goes here...
// ...
}
}