如何修复的方法startActivity(意向)是未定义的类型新View.OnClickListener()语法错误 [英] How to fix The method startActivity(Intent) is undefined for the type new View.OnClickListener() syntax error
问题描述
我有一个语法错误与我的code,在getView我想使听者按钮更新移动到另一个活动:
I have a syntax errors with my code , in the "getView" I want to make a listener for the button "update" to move to another activity :
@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;
}
我试图解决关于意图这些错误,但我失败了:(
I've tried to fix these errors about "Intent" but I failed :(
-
该方法startActivity(意向)是未定义的类型新View.OnClickListener()
The method startActivity(Intent) is undefined for the type new View.OnClickListener()
该方法getApplicationContext()是未定义的类型新View.OnClickListener()
The method getApplicationContext() is undefined for the type new View.OnClickListener()
甚至whene我感动从的onClick这些语句的方法的问题并没有改变! 我进口的意图库,如何解决????
and even whene I moved these statements from "onClick" method the problem didn't change !! I imported "Intent" library , how to solve that ????
推荐答案
如果您的适配器是在不同的文件,您需要的活动环境。
If your adapter is in a different file you will need activity context.
您需要从活动类传递活动的背景下,以您的适配器的构造。
You need to pass the activity context from your activity class to your adapter constructor.
<一个href="http://developer.android.com/reference/android/content/Context.html#startActivity(android.content.Intent)">http://developer.android.com/reference/android/content/Context.html#startActivity(android.content.Intent)
startActivity
是活动类的方法。所以,你需要的活动范围内展开活动。
startActivity
is a method of your activity class. So you need activity context to start the activity.
此外 getApplicationContext
使用活动方面。
<一个href="http://stackoverflow.com/questions/7298731/when-to-call-activity-context-or-application-context">When调用活动上下文或应用程序上下文?
由commonsware检查答案。
Check the answer by commonsware.
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent redirect = new Intent(context,Update.class);
context.startActivity(redirect);
}
});
这篇关于如何修复的方法startActivity(意向)是未定义的类型新View.OnClickListener()语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!