1

I am new here, so I apologise if this question is somewhat trivial!

I'm trying to set up a clickable ImageView to link to a website, and am getting a syntax error on my setOnClickListener, which I can't figure out. I'm sure it's a straight forward error, but if someone could just point me in the right direction, it would be greatly appreciated!

Here's my code below:

public class VideoActivity extends Activity {
    ImageView blipImg = (ImageView)findViewById(R.id.videoBlip);
    blipImg.setOnClickListener = (new View.OnClickListener(){
        public void onClick(View v){
             Intent intent = new Intent();
             intent.setAction(Intent.ACTION_VIEW);
             intent.addCategory(Intent.CATEGORY_BROWSABLE);
             intent.setData(Uri.parse("http://blip.tv"));
            startActivity(intent);
        }
    });
}
the-ginger-geek
  • 7,041
  • 4
  • 27
  • 45
Yugen
  • 33
  • 1
  • 7
  • 3
    remove the = before the (new View.OnClickListener – the-ginger-geek Nov 13 '13 at 11:32
  • You can also define the onclicklistener in xml. Here's an example [link](http://stackoverflow.com/questions/4153517/how-exactly-does-the-androidonclick-xml-attribute-differ-from-setonclicklistene) – Fabian Nov 13 '13 at 11:39

7 Answers7

2

Change to

  blipImg.setOnClickListener(new View.OnClickListener(){ // remove =

and add @Override annotation

blipImg.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v){
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_BROWSABLE);
        intent.setData(Uri.parse("http://blip.tv"));
        startActivity(intent);
        }
   });

And move the code to onCreate as below

public class VideoActivity extends Activity {
ImageView blipImg;
@Override
protected void onCreate(Bundle savedInstanceState) 
{
   super.onCreate(savedInstanceState);
   setContentView(R.layout.yourlayout);
   blipImg = (ImageView)findViewById(R.id.videoBlip);
   blipImg.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v){
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_BROWSABLE);
        intent.setData(Uri.parse("http://blip.tv"));
        startActivity(intent);
        }
   });
 }
}
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
2

Your code should be in a method body e.g. onCreate() and not in class body.

Also remove the = after setOnClickListener.

Example:

public class VideoActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstance) {
    super.onCreate(savedInstance);

    setContentView(R.layout.some_layout_that_contains_videoBlip);

    ImageView blipImg = (ImageView)findViewById(R.id.videoBlip);
    blipImg.setOnClickListener(new View.OnClickListener(){
      public void onClick(View v){
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_BROWSABLE);
        intent.setData(Uri.parse("http://blip.tv"));
        startActivity(intent);
      }
    });
  }
}
laalto
  • 150,114
  • 66
  • 286
  • 303
0

Do this :

blipImg.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    intent.addCategory(Intent.CATEGORY_BROWSABLE);
    intent.setData(Uri.parse("http://blip.tv"));
    startActivity(intent);
}
});
Siddharth_Vyas
  • 9,972
  • 10
  • 39
  • 69
0

use this "=" sign will not come in setonclicklistener

    public class VideoActivity extends Activity {
ImageView blipImg = (ImageView)findViewById(R.id.videoBlip);
blipImg.setOnClickListener(new View.OnClickListener(){
    public void onClick(View v){
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_BROWSABLE);
        intent.setData(Uri.parse("http://blip.tv"));
        startActivity(intent);
    }
});
}
Beginner
  • 1,414
  • 2
  • 21
  • 41
0

Replace your code with this

blipImg.setOnClickListener(new OnClickListener() 
{
    @Override
    public void onClick(View v) 
    {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_BROWSABLE);
        intent.setData(Uri.parse("http://blip.tv"));
        startActivity(intent);
    }
});
Sanket Shah
  • 4,352
  • 3
  • 21
  • 41
0

Put your code inside onCreate method. And setcontentview and then do these steps. Because a java class should have only variables and methods, All definitions should be inside a method.

public class VideoActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.your_layout); //your layout which contains imageview with id R.id.videoBlip
   ImageView blipImg = (ImageView)findViewById(R.id.videoBlip);
   blipImg.setOnClickListener (new View.OnClickListener(){ //remove = symbol
      public void onClick(View v){
         Intent intent = new Intent();
         intent.setAction(Intent.ACTION_VIEW);
         intent.addCategory(Intent.CATEGORY_BROWSABLE);
         intent.setData(Uri.parse("http://blip.tv"));
         startActivity(intent);
      } 
   });
}
}

I hope this will help you.

Gunaseelan
  • 14,415
  • 11
  • 80
  • 128
0

please remove"=" in your code and try this

public class VideoActivity extends Activity {
  @Override
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);

setContentView(R.layout.yourlayout);

ImageView blipImg = (ImageView)findViewById(R.id.videoBlip);
blipImg.setOnClickListener(new View.OnClickListener(){
  public void onClick(View v){
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    intent.addCategory(Intent.CATEGORY_BROWSABLE);
    intent.setData(Uri.parse("http://blip.tv"));
    startActivity(intent);
  }
});

} }

Invader
  • 679
  • 3
  • 10