如何修复方法startActivity(Intent)对于新的View.OnClickListener()语法错误类型未定义 [英] How to fix The method startActivity(Intent) is undefined for the type new View.OnClickListener() syntax error

查看:622
本文介绍了如何修复方法startActivity(Intent)对于新的View.OnClickListener()语法错误类型未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个语法错误与我的代码,在getView我想让一个监听器的按钮更新移动到另一个活动:



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


LayoutInflater l =(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE) ;
查看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;
}

我试图修复这些关于意图的错误,但是我失败了: (


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


  2. 方法getApplicationContext()对于新的类型未定义View.OnClickListener()


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

解决方案

如果您的适配器位于不同的文件中,您将需要活动上下文。



将活动上下文从您的活动类传递给适配器构造函数。



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



startActivity 是您的活动类的一种方法。所以你需要活动上下文来启动活动。



而不是使用 getApplicationContext 使用活动上下文。

何时调用活动上下文OR应用程序上下文?



通过commonsware检查答案。

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

意向重定向=新意图(上下文,更新) class);
context.startActivity(redirect);
}
});


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 :(

  1. The method startActivity(Intent) is undefined for the type new View.OnClickListener()

  2. The method getApplicationContext() is undefined for the type new View.OnClickListener()

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.

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

startActivity is a method of your activity class. So you need activity context to start the activity.

Also instead of getApplicationContext use activity context.

When to call activity context OR application context?

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(Intent)对于新的View.OnClickListener()语法错误类型未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆