【问题标题】:Errors in Layout Cant figure it out布局中的错误无法弄清楚
【发布时间】:2010-09-25 10:30:31
【问题描述】:

我创建了四个选项卡,每个选项卡都带有列表视图。我一直在尝试使列表视图可点击,我使用Here 的列表视图教程使用 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"};

语法错误,插入“}”完成 类体

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

我在这里收到这些错误:

  public void onListItemClick(ListView, parent, View v, int position, long id); }
此行有多个标记 - 标记的语法错误,错误的构造 - 语法错误,插入“;”完成 LocalVariableDeclarationStatement - 标记 ",", 的语法错误;预期的 - 标记 "(", = 预期的语法错误 - 标记 ",", 的语法错误;预期的 - 语法错误,插入“}”完成MethodBody - 语法错误,插入“}”完成ClassBody - 标记“}”的语法错误,删除此标记

同样的问题,我尝试了不同的组合,但在这个设置中它不断给我错误,我的错误最少

非常感谢任何帮助

【问题讨论】:

    标签: java android eclipse


    【解决方案1】:

    一切都很好,直到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 行:您无法将 int (position) 与 String 进行比较
    • 第 7-10 行:这些必须在内部方法定义中,例如将它们移到第 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);
        }
    }
    

    【讨论】:

    • 非常感谢你,我是第一个承认嵌入在这个问题上苦苦挣扎的人,但是在关于这个主题绑架的问题之后,你给的代码有点混乱,我终于设法让它工作天才克里斯 ....天才
    • P.s 拼写是我的手机预测文本 9 个问题我的意思
    猜你喜欢
    • 2015-10-26
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 2016-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多