注释掉“@Override”是否安全?
为了编译我的代码 - 其中包含以下内容:
public class ContactsActivity extends ListActivity implements AdapterView.OnItemClickListener {
Cursor mContacts;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Return all contacts, ordered by name
String[] projection = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME };
mContacts = managedQuery(ContactsContract.Contacts.CONTENT_URI,
projection, null, null, ContactsContract.Contacts.DISPLAY_NAME);
// Display all contacts in a ListView
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, mContacts,
new String[] { ContactsContract.Contacts.DISPLAY_NAME },
new int[] { android.R.id.text1 });
setListAdapter(mAdapter);
// Listen for item selections
getListView().setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
. . .
...我必须注释掉 onItemClick() 事件处理程序上方的“@Override”。这安全吗?我应该耸耸肩说:“好吧,那行了,我会忘记它”还是有邪恶正在发生/隐藏的问题潜伏着?
To get my code to compile - which contained the following:
public class ContactsActivity extends ListActivity implements AdapterView.OnItemClickListener {
Cursor mContacts;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Return all contacts, ordered by name
String[] projection = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME };
mContacts = managedQuery(ContactsContract.Contacts.CONTENT_URI,
projection, null, null, ContactsContract.Contacts.DISPLAY_NAME);
// Display all contacts in a ListView
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, mContacts,
new String[] { ContactsContract.Contacts.DISPLAY_NAME },
new int[] { android.R.id.text1 });
setListAdapter(mAdapter);
// Listen for item selections
getListView().setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
. . .
...I had to comment out the "@Override" above the onItemClick() event handler. Is this safe? Should I just shrug my shoulders and say, "OK, that works, I'll forget about it" or is there evil afoot/a hidden problem lurking?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
根据我的建议,删除该行不好。你需要在你的eclipse中更改JDK版本,这样你就不会出现这样的错误。按照以下步骤操作,
右键单击您的项目并单击它。选择属性。
然后从左侧部分选择 Java Compiler &选择您安装的 JDK 版本。如果已选择,则取消选择并取消选择。尝试一下。
as per my suggestion, it is not good to remove that line. you need to change to JDK version in your eclipse then you will not get such errors. Follow, following steps for it,
Right Click on your Project & select Properties.
Then from the left section Select Java Compiler & select the Version of the JDK you installed. If it is already selected then de-select it & try it.
如果您必须注释掉
@Override
,那么您的编译器的 JDK 合规级别未设置为 1.6,而 Android 开发应设置为 1.6。如果您使用的是 Eclipse,请右键单击项目名称并选择“属性”。然后选择“Java Compiler”并根据需要进行调整。 (您可能从工作区继承了较低的 JDK 合规性级别。如果合适,请更改工作区的合规性级别。这样就不必在新项目中修复相同的问题。)但是要回答您的问题:注释掉是安全的
@Override
。使用较低的合规级别并不是那么好。If you had to comment out the
@Override
, then your compiler's JDK compliance level isn't set to 1.6, which it should be for Android development. If you're using Eclipse, right click on the project name and select "Properties". Then select "Java Compiler" and adjust as necessary. (You probably inherited a lower JDK compliance level from the workspace. If appropriate, change the compliance level for the workspace instead. This will save having to fix the same problem with new projects.)But to answer your question: it's safe to comment out
@Override
. It's just not so great to be using a lower compliance level.你的java版本设置为低于1.6。从这里开始,接口(而不仅仅是方法)可以作为 Override 进行销售。右键单击项目->属性-> Java编译器->编译器合规级别为 1.6
You java version is set to less than 1.6. Starting there, interfaces and not just methods can be market as Override. Right click the project -> Properties -> Java Compiler -> Compiler compliance level to 1.6
来自 Java 文档:
From the Java docs:
这取决于。如果您的编译器设置为目标 java 5,并且 onCreate 方法是从接口继承的,那么可以。 Java 5 不支持接口方法上的 override 注解,而 Java 6 则支持。这本身可能表明您的 JDK 设置错误,如果您希望以 Java 6 为目标,那么您得到的可能是 Java 5。
It depends. If your compiler is set to target java 5, and the onCreate method is inherited from the interface then yes. Java 5 doesn't support the override annotation on interface methods, whereas Java 6 expects it. This may itself be an indication that your JDK is set up wrong, if you're expecting to target Java 6, what you may be getting is Java 5.
如果您将其注释掉,则不会覆盖这些方法。所以你实际上什么也不做。 (我的意见)
if you'd commented it out, you would not override the methods. So you'd actually do nothing. (My opinion)