布局中的错误无法弄清楚

时间:2010-09-25 10:30:31

标签: java android eclipse

我创建了四个带有listview的标签。我一直在尝试使列表视图可单击,我使用了Here的listview教程,使用string.xml和R.array创建列表视图:

问题是当我使用我的意图和onItemClickListener时,我得到多个标记错误,如果我使用逗号括号和类体标记来处理错误,那么它是语法问题还是布局或代码的位置;

public class ll2 extends ListActivity {

    static final String[] teams = new String[] {"Accrington Stanley", "Aldershot", "Barnet", "Bradford City", "Burton Albion", "Bury", "Cheltenham Town", "Chesterfield", "Crewe Alexandra"};


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final String[] TEAMS = getResources().getStringArray(R.array.twoteams_array);
        setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, TEAMS));

        ListView lv = getListView();
        lv.setTextFilterEnabled(true);

        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
              int position, long id) {

            public void onListItemClick(ListView, parent, View v, int position, long id);
            }

            if (position == "Braford City") {
                Intent intent = new Intent(this, Bradford.class);
                startActivity(intent);
            }

}

我在这里得到这些错误:

static final String[] teams = new String[] {"Accrington Stanley", "Aldershot", "Barnet", "Bradford City", "Burton Albion", "Bury", "Cheltenham Town", "Chesterfield", "Crewe Alexandra"};
  

语法错误,插入“}”即可完成   ClassBody

如果我添加到完整的课堂主体,我会在这里和其他地方获得更多的错误。

我在这里得到这些错误:

  public void onListItemClick(ListView, parent, View v, int position, long id); }
Multiple markers at this line
    - Syntax error on token(s), misplaced construct(s)  
    - Syntax error, insert ";" to complete LocalVariableDeclarationStatement  
    - Syntax error on token ",", ; expected
    - Syntax error on token "(", = expected
    - Syntax error on token ",", ; expected
    - Syntax error, insert "}" to complete MethodBody
    - Syntax error, insert "}" to complete ClassBody
    - Syntax error on token "}", delete this token

同样的问题在这里我尝试了不同的组合,它通过这种设置不断给我错误我有最少的错误

任何帮助非常感谢

1 个答案:

答案 0 :(得分:2)

一切都很好,直到setOnItemClickListener,它变得一团糟。

 1  lv.setOnItemClickListener(new OnItemClickListener() {
 2      public void onItemClick(AdapterView<?> parent, View view, 
                                int position, long id) {
 3
 4      public void onListItemClick(ListView, parent, View v,
                                    int position, long id);
 5      }
 6
 7      if (position == "Braford City") {
 8          Intent intent = new Intent(this, Bradford.class);
 9          startActivity(intent);
10      }
11
12
  • 第2行:您未关闭onItemClick方法定义,因此请添加大括号}
  • 第4行:不应以分号;结尾,而应以开放式大括号{
  • 结束
  • 第7行:您无法将intposition)与String
  • 进行比较
  • 第7-10行:这些必须 in 方法定义,例如将它们移到第5行之前
  • 第11行:您需要关闭在第1行打开的new OnItemClickListener()setOnItemClickListener来电,方法是添加:});
  • 第12行:您需要使用大括号ll2关闭班级}

此外,ListActivity已经附带了onListItemClick方法,因此您不需要onCreate中的上述代码 - 无需定义自己的侦听器。

onCreate之后添加一个新方法:

public void onListItemClick(ListView l, View v, int position, long id) {
    if (position == 3) {
        Intent intent = new Intent(this, Bradford.class);
        startActivity(intent);
    }
}