开发者

Can't start Activity when pressing an element on GridView

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);

 开发者_如何学编程                   }
                }

              });


The problem is because of poor reference to Context. Try this instead,

 Intent intent = new Intent(imageView.getRootView().getContext(), 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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜