gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 07:05:56 25 4
gpt4 key购买 nike

我是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com