原文转载自:http://jlins.iteye.com/blog/560891
上下文菜单Context Menu
Android的上下文菜单在概念上和PC软件的右键菜单类似。当一个视图注册到一个上下文菜单时,执行一个在该对象上的“长按”(按住不动差不多两秒钟)动作,将出现一个提供相关功能的浮动菜单。上下文菜单可以被注册到任何视图对象中,不过,最常见的是用于列表视图ListView的item,在按中列表项时,会转换其背景色而提示将呈现上下文菜单。 (电话联系人列表提供了关于这个特性的一个很好的例子)。
注意:上下文菜单项不支持图标或快捷键。
为了创建一个上下文菜单,你必须重写这个活动的上下文菜单回调函数:onCreateContextMenu() 和onContextItemSelected()。在回调函数onCreateContextMenu()里,你可以通过使用一个add()方法来添加菜单项,或者通过扩充一个定义在XML中的菜单资源。然后,通过registerForContextMenu()为这个视图注册一个上下文菜单ContextMenu.
比如,下面的代码可以被用到Notepad应用程序中来为列表中的每一个注释添加一个上下文菜单:
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); menu.add(0, EDIT_ID, 0, "Edit"); menu.add(0, DELETE_ID, 0, "Delete"); } public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); switch (item.getItemId()) { case EDIT_ID: editNote(info.id); return true; case DELETE_ID: deleteNote(info.id); return true; default: return super.onContextItemSelected(item); } }
在onCreateContextMenu()中,除了给出将添加MenuItems的ContextMenu外,还需要给定选中的视图和一个上下文菜单信息ContextMenuInfo对象,该对象提供了选中对象的附加信息。在本例中,onCreateContextMenu()没做什么特别的事-只是添加了一些菜单项。在onContextItemSelected() 回调函数中,我们从MenuItem中请求AdapterContextMenuInfo,该对象提供当前选中项的信息。我们从中所要的只是这个选中项的列表ID,所以无论编辑还是删除一个注释,我们通过这个对象的AdapterContextMenuInfo.info字段来找到该ID。这个ID被传递给editNote() 和deleteNote()方法来执行相应的动作。
现在,要为一个列表视图中的所有项注册上下文菜单,我们可以传递整个的列表视图对象给registerForContextMenu(View) 方法:
registerForContextMenu(getListView());
记住,你可以传递任何视图对象来注册一个上下文菜单。这里,getListView()返回这个被用于Notepad应用程序的列表活动ListActivity中的列表视图对象。这样,这个列表中的任何item都被注册到这个上下文菜单。
这是我第二次遇到这个错误,导致程序无法运行,两次均是在我导入外部jar包后发生的错误
2010-12-11 01:12:28 - myplayer] trouble processing "java/net/DatagramPacket.class": [2010-12-11 01:12:28 - myplayer] Attempt to include a core class (java.* or javax.*) in something other than a core library. It is likely that you have attempted to include in an application the core library (or a part thereof) from a desktop virtual machine. This will most assuredly not work. At a minimum, it jeopardizes the compatibility of your app with future versions of the platform. It is also often of questionable legality. If you really intend to build a core library -- which is only appropriate as part of creating a full virtual machine distribution, as opposed to compiling an application -- then use the "--core-library" option to suppress this error message. If you go ahead and use "--core-library" but are in fact building an application, then be forewarned that your application will still fail to build or run, at some point. Please be prepared for angry customers who find, for example, that your application ceases to function once they upgrade their operating system. You will be to blame for this problem. If you are legitimately using some code that happens to be in a core package, then the easiest safe alternative you have is to repackage that code. That is, move the classes in question into your own package namespace. This means that they will never be in conflict with core system classes. If you find that you cannot do this, then that is an indication that the path you are on will ultimately lead to pain, suffering, grief, and lamentation. [2010-12-11 01:12:28 - myplayer]1 error; aborting [2010-12-11 01:12:28 - myplayer]Conversion to Dalvik format failed with error 1
最后的解决办法 是 修改.classpath 文件
只要使 .classpath 文件 中的path属性值为正确的jar路径即可
红色部分为我引用的外部jar包 路径,保证这些路径正确且唯一即可
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gen"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry kind="lib" path="lib/cpdetector_1.0.7.jar"/>
<classpathentry kind="lib" path="lib/antlr.jar"/>
<classpathentry kind="lib" path="lib/chardet.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
另外博客 http://fancyboy2050.iteye.com/blog/745059中介绍了 因为 sdk版本问题引发上述错误的解决办法,可以学习一下