0

I have the following code but cannot seem to figure out why I am getting the above error message. I obviously know what it means but I've checked the brackets and they seem correct to me. I'm running ADT 21.1.0. So far I've tried restarting Eclipse; restarting my PC; and re-building the project.

package cct.mad.lab;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class WalkList extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // storing string resources into Array
        String[] players = getResources().getStringArray(R.array.players);

        // Binding resources Array to ListAdapter
        this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label, players));
    }

    protected void onListItemClick (ListView 1, View v, int position, long id) {
        String item = (String) getListAdapter().getItem(position);
    Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
    }
}
7
  • 4
    at which line you getting this error Commented Mar 17, 2013 at 22:59
  • 2
    Are you really trying to call that ListView parameter "1"? I'd think that might be confusing the compiler somewhat. Commented Mar 17, 2013 at 23:00
  • ListView 1, eclipse is surely warning you about that Commented Mar 17, 2013 at 23:02
  • 2
    1 is no valid variable name. Commented Mar 17, 2013 at 23:03
  • 1
    @Ciaran No, seriously, it's not. And I think it's probably what's confusing the compiler. Really, change the name from "1" to "lv" or something temporarily and see if the problem goes away. Commented Mar 17, 2013 at 23:06

2 Answers 2

3

This is almost certainly because of an invalid identifier defined in the onListItemClick definition.

Change the ListView identifier in onListItemClick from 1 to something else (a valid identifier).

From jls-3.8:

An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter.

More about the Java letter:

The "Java letters" include uppercase and lowercase ASCII Latin letters A-Z (\u0041-\u005a), and a-z (\u0061-\u007a), and, for historical reasons, the ASCII underscore (_, or \u005f) and dollar sign ($, or \u0024). The $ character should be used only in mechanically generated source code or, rarely, to access pre-existing names on legacy systems.

Sign up to request clarification or add additional context in comments.

Comments

0

The compiler is being confused by the use of "1" as an identifier. This answer hopefully gives some insight as to why variable names in Java can't start with a number, but fundamentally, it's in the language spec.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.