Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in Android Developers Tutorial?

Tags:

android

I am working my way through some of the android developers tutorial, specifically the Gallery View Widget located here I have made it through stages 1 and 2 yet I seem to be getting 3 errors and I cannot fathom what it is I have done wrong, as I have copied and pasted the code direct from the tutorial and made one change to the code, that being R.layout.events instead of R.layout.main

Here is the code

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.Toast;

public class Events extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.events);

    Gallery gallery = (Gallery) findViewById(R.id.gallery1);
    gallery.setAdapter(new ImageAdapter (this));

    gallery.setOnClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position, long id) {
            Toast.makeText(Events.this, "" + position, Toast.LENGTH_LONG).show();
        }
    });
}}

The errors that I am getting are as follows:

ImageAdapter cannot be resolved to a type line 18 Java Problem OnItemClickListener cannot be resolved to a type line 20 Java Problem

The method setOnClickListener(View.OnClickListener) in the type AdapterView is not applicable for the arguments (new OnItemClickListener(){}) line 20 Java Problem

Any advice or pointers would be greatly appreciated. Thanks in Advance.

like image 923
Qu1nncunxIV Avatar asked Jan 23 '26 06:01

Qu1nncunxIV


2 Answers

The ImageAdapter is a custom BaseAdapter defined further in the post, at point 6.

A custom BaseAdapter called ImageAdapter is instantiated (...)

And at point 6:

public class ImageAdapter extends BaseAdapter { //(...)
like image 100
nhaarman Avatar answered Jan 25 '26 21:01

nhaarman


Wrong listener type. It should be View.OnClickListener.

gallery.setOnClickListener(new View.OnClickListener() { ....

Or use setOnItemClickListener...

gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() { ...
like image 36
pzulw Avatar answered Jan 25 '26 21:01

pzulw