java - onListItemClick()错误: it must override or implement a supertype method

标签 java android-fragments android-adapter

我是 Android 和 Java 的初学者。我搜索了此错误的答案,但 stackOverflow 中没有任何内容适合我的情况。我正在关注一本书,其中说我们需要使用 onListItemClick() 方法,该方法来 self 们创建的扩展 ListFragment 的类 (CrimeListFragment)(必须使用该方法和类)。

但是,我不断收到错误:

@Override
    public void onListItemClick(ListView 1, View v, int position, long id)
    {
        Crime c = (Crime)(getListAdapter()).getItemViewType(position);
        Log.d(TAG, c.getTitle() + " was clicked");
    }

错误提示

"The method on onListItemClick of type CrimeListFragment must override or implement a supertype method."

ListView 1 参数也存在问题。这也显示了一个错误。

这个项目很复杂,有很多类,但这里是这个页面的主要代码:

package com.bignerdranch.android.criminalintent;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.widget.ArrayAdapter;
import android.widget.ListView;


public class CrimeListFragment extends ListFragment {
    private static final String TAG = "CrimeListFragment";
    private ArrayList<Crime> mCrimes;



    @Override
    public void onListItemClick(ListView 1, View v, int position, long id)
    {
        Crime c = (Crime)(getListAdapter()).getItemViewType(position);
        Log.d(TAG, c.getTitle() + " was clicked");
    }

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        getActivity().setTitle(R.string.crimes_title);
        mCrimes = CrimeLab.get(getActivity()).getCrimes();

        ArrayAdapter<Crime> adapter = new ArrayAdapter<Crime>(getActivity(),
                android.R.layout.simple_list_item_1, mCrimes);

        setListAdapter(adapter);
    }

}

如何消除这两个错误?谢谢。

最佳答案

在 Java 中为变量或方法参数命名时,应遵循以下规则:

Variable names are case-sensitive. A variable's name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign "$", or the underscore character "". The convention, however, is to always begin your variable names with a letter, not "$" or "". Additionally, the dollar sign character, by convention, is never used at all. You may find some situations where auto-generated names will contain the dollar sign, but your variable names should always avoid using it. A similar convention exists for the underscore character; while it's technically legal to begin your variable's name with "_", this practice is discouraged. White space is not permitted.

Subsequent characters may be letters, digits, dollar signs, or underscore characters. Conventions (and common sense) apply to this rule as well. When choosing a name for your variables, use full words instead of cryptic abbreviations. Doing so will make your code easier to read and understand. In many cases it will also make your code self-documenting; fields named cadence, speed, and gear, for example, are much more intuitive than abbreviated versions, such as s, c, and g. Also keep in mind that the name you choose must not be a keyword or reserved word.

If the name you choose consists of only one word, spell that word in all lowercase letters. If it consists of more than one word, capitalize the first letter of each subsequent word. The names gearRatio and currentGear are prime examples of this convention. If your variable stores a constant value, such as static final int NUM_GEARS = 6, the convention changes slightly, capitalizing every letter and separating subsequent words with the underscore character. By convention, the underscore character is never used elsewhere.

您使用“1”作为参数名称显然违反了它们。重命名以符合命名规则,应该可以解决两个编译问题。

有关更多信息,请参阅 here

关于java - onListItemClick()错误: it must override or implement a supertype method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21374285/

相关文章:

java - QR 码扫描仪库支持 Marshmallow?

java - 向 TitleAreaDialog 添加一个关键监听器

android - fragment 布局中的 TextView 在调用 SetText() 时不会更改

android - 适配器两次返回相同的项目

android - 快速点击(点击)RecyclerView 打开多个 Fragments

Java Servlet 大量请求和线程

java - 将父类(super class)作为参数传递给需要子类的方法

android - 在滑动 Pane 布局中单击时将 fragment 带到 'front'

java - 通过 NavigationComponent 在 BottomNavigation fragment 之间传递数据

java - 如何修复在新 Activity 上呈现为空白的 TextView