'onListItemClick'方法让Eclipse给我错误

时间:2011-07-25 12:02:39

标签: java android eclipse programming-languages

我正在阅读针对傻瓜的Android应用程序开发,我正在第9章,我正在编写一个任务提醒应用程序。我有一个onListItemClick方法,但Eclipse不断给出错误....

package com.dummies.android.taskreminder;

import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.*;

    public class ReminderListActivity extends ListActivity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.reminder_list);




            String[] items = new String[] { "Foo", "Bar", "Fizz", "Bin" };


            ArrayAdapter<String> adapter =
                new ArrayAdapter<String>(this, R.layout.reminder_row, R.id.text1, items);
            setListAdapter(adapter); 

            @Override
            protected void onListItemClick(ListView l, View v, int position, long id) {
                super.onListItemClick(l, v, position, id);
            }


        }
    }

我的错误:my error

Eclipse说: “视图无法解析为某种类型” “令牌上的语法错误.....预期”(5x) “void是变量onListItemClick的无效类型”

我做错了什么?

3 个答案:

答案 0 :(得分:3)

试试这个

package com.dummies.android.taskreminder;

import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.*;

    public class ReminderListActivity extends ListActivity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.reminder_list);

            String[] items = new String[] { "Foo", "Bar", "Fizz", "Bin" };

            ArrayAdapter<String> adapter =
                new ArrayAdapter<String>(this, R.layout.reminder_row, R.id.text1, items);
            setListAdapter(adapter); 
        }

        @Override
        protected void onListItemClick(ListView l, View v, int position, long id) {
            super.onListItemClick(l, v, position, id);
        }

    }

答案 1 :(得分:2)

您已将onListItemClick方法放在onCreate方法中。将它移到该方法之外。

您可能还缺少导入语句。

答案 2 :(得分:1)

您正试图覆盖onListItemClick方法中的onCreate方法。您需要将该代码置于onCreate()方法之外。