Android 安卓McClick无声地失败了

Android 安卓McClick无声地失败了,android,Android,我通过ListAdapter在列表中显示一些sql数据。除了尝试为列表中的每个项目设置一个单击侦听器外,其他一切都正常工作。当我单击任何项目时,都不会发生任何事情;没有错误消息,只是默默地失败了 public class Notes extends ListActivity implements OnClickListener { private static final String TAG = "Notes"; private NotesData notes; pu

我通过ListAdapter在列表中显示一些sql数据。除了尝试为列表中的每个项目设置一个单击侦听器外,其他一切都正常工作。当我单击任何项目时,都不会发生任何事情;没有错误消息,只是默默地失败了

public class Notes extends ListActivity implements OnClickListener {
    private static final String TAG = "Notes";
    private NotesData notes;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        notes = new NotesData(this);
        try {
            Cursor cursor = getNotes();
            showNotes(cursor); /* set the cursor to the listadapter */
            ListView ls = (ListView) findViewById(android.R.id.list);
            ls.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent,View v,
                    int position,long id) {
                    Toast.makeText(getApplicationContext(),"clicked",
                        Toast.LENGTH_SHORT).show();
                }
            });
        } finally {
            notes.close();
        }

    }
main.xml,包含listview:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/new_note_button"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="@string/new_note"/>
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
  android:id="@+id/empty"
  android:layout_height="wrap_content"
  android:layout_width="wrap_content"
  android:text="@string/empty"/>
</LinearLayout>

是否有我遗漏的内容?

没有遗漏任何内容,您的代码完全正确,只需将getApplicationContext替换为,这可能会解决问题,因为您需要传递的是对当前组件上下文的引用,而不是对当前进程上下文的引用

希望能有帮助。
Hass.

我想您的列表项设置为可点击。view.setClickabletrue


当列表项可单击时,将调用onItemClick。

我建议在try语句中抛出类似的内容

try{
    //yourstuff
} catch (Exception e) {
    e.printStackTrace();
}
然后检查抛出的错误消息。当它设置onClickListener时,可能会抛出错误,可能存在无效的内容,但只要所有这些都在try语句中,它就不会给您任何有用的错误消息。因此,您也可以从try语句中提取尽可能多的内容,直到找到问题-

您需要将android:focusable=false添加到main.xml文件中的按钮中。该按钮将焦点从列表项移开


这肯定能解决您的问题。

还要检查列表视图是否设置为已启用、可单击和可聚焦。仅供参考,android:id=@+id/empty应为android:id=@android:id/emptyonItemClick未被调用。将listview设置为clickable(可单击)和focusable(可聚焦)没有帮助。只需在onitemclick事件中添加一些日志,然后查看日志是否已打印?这就是您了解问题所在的方式。在click或toastI中,我已经这样做了,这就是我知道它没有被调用的原因。它是否需要是Notes。这?好吧,makeText调用没有问题,就像以前我在它的位置上有一个简单的Log.d调用一样,这也不起作用。