无法启动时pressing在GridView控件元素活动 [英] Can't start Activity when pressing an element on GridView

查看:163
本文介绍了无法启动时pressing在GridView控件元素活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要的是点击在GridView任何图像时启动活动。
我maked这一点,但我对 getApplicationContext错误()

 的方法getApplicationContext()是未定义的类型新View.OnClickListener(){}

code:

  imageView.setOnClickListener(新View.OnClickListener(){                @覆盖
                公共无效的onClick(查看视图){
                    如果(位置== 0){
                         意向意图=新意图(getApplicationContext(),carburant.class);
                         startActivity(意向);                    }
                }              });


解决方案

您有一个范围的问题。查看没有getApplicationContext() - 必须访问父活动的范围,以获取应用程序上下文

 意向意图=新意图(MyParentActivity.this.getApplicationContext(),carburant.class);

i want to start an Activity when any Image from the GridView is clicked. I maked this but i have an error on getApplicationContext() :

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

Code:

imageView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    if (position==0) {
                         Intent intent = new Intent(getApplicationContext(), carburant.class);
                         startActivity(intent);

                    }
                }

              });

解决方案

You have a scope issue. View does not have a getApplicationContext() - you must access the parent activity's scope to get the app context.

Intent intent = new Intent(MyParentActivity.this.getApplicationContext(), carburant.class);

这篇关于无法启动时pressing在GridView控件元素活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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