gpt4 book ai didi

android - 如何修复 The method startActivity(Intent) is undefined for the type new View.OnClickListener() 语法错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:52:38 25 4
gpt4 key购买 nike

我的代码有语法错误,在“getView”中我想为按钮“更新”创建一个监听器以移动到另一个 Activity :

enter image description here

@Override
public View getView(int position, View convertView, ViewGroup parent) {


LayoutInflater l = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View rowView = l.inflate(R.layout.temp, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.textView1);
ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView1);
Button update = (Button) rowView.findViewById(R.id.button1);

// update.setOnClickListener(new View.OnClickListener() {
//
// @Override
// public void onClick(View v) {
//
// Intent redirect = new Intent(getApplicationContext(),Update.class);
// startActivity(redirect);
//
// }
// });




textView.setText(sa.get(position));


return rowView;
}

我试图修复这些关于“Intent”的错误,但我失败了:(

  1. 未为类型 new View.OnClickListener() 定义方法 startActivity(Intent)

  2. 未为新 View.OnClickListener() 类型定义方法 getApplicationContext()

甚至当我从“onClick”方法中移动这些语句时,问题也没有改变!!我导入了“Intent”库,如何解决????

最佳答案

如果您的适配器在不同的文件中,您将需要 Activity 上下文。

您需要将 Activity 上下文从您的 Activity 类传递到您的适配器构造函数。

http://developer.android.com/reference/android/content/Context.html#startActivity(android.content.Intent)

startActivity 是您的 Activity 类的一个方法。因此,您需要 Activity 上下文来启动 Activity 。

也可以使用 Activity 上下文代替 getApplicationContext

When to call activity context OR application context?

通过commonsware检查答案。

     update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Intent redirect = new Intent(context,Update.class);
context.startActivity(redirect);
}
});

关于android - 如何修复 The method startActivity(Intent) is undefined for the type new View.OnClickListener() 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17505969/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com