我有一个listview,我使用自制类填充数据。它就像一个魅力。我想添加一个功能来触摸其中一个列表项。因此,我使用onClick指令添加一个调用,如下所示:
<?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"
>
<ListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:onClick="ScanBearbeiten"
/>
<TextView
android:id="@+id/tx_NoScans"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/main_no_items"
/>
</LinearLayout>
但是,只要我添加上面看到的onClick指令,应用程序就会停止工作。布局的setContentView()将停止,并显示以下堆栈跟踪:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.cisgmbh.app/de.cisgmbh.app.ScansBearbeiten}: android.view.InflateException: Binary XML file line #7: Error inflating class android.widget.ListView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class android.widget.ListView
at android.view.LayoutInflater.createView(LayoutInflater.java:513)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
at android.app.Activity.setContentView(Activity.java:1647)
at de.cisgmbh.app.ScansBearbeiten.onCreate(ScansBearbeiten.java:24)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
... 11 more
Caused by: java.lang.reflect.InvocationTargetException
at android.widget.ListView.<init>(ListView.java:153)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
at android.view.LayoutInflater.createView(LayoutInflater.java:500)
... 22 more
Caused by: java.lang.RuntimeException: Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead
at android.widget.AdapterView.setOnClickListener(AdapterView.java:750)
at android.view.View.<init>(View.java:2046)
at android.view.ViewGroup.<init>(ViewGroup.java:291)
at android.widget.AdapterView.<init>(AdapterView.java:228)
at android.widget.AbsListView.<init>(AbsListView.java:527)
at android.widget.ListView.<init>(ListView.java:157)
... 26 more
我知道如何解决这个问题,但我很感兴为什么会这样。是否无法为列表视图定义静态onClick侦听器? Eclipse中的设计器将此作为有效功能提供。或者这是一个错误吗?
感谢。
答案 0 :(得分:0)
您需要拨打setOnItemClickListener
而不是onclicklistener。对于按钮,您可以使用onItemCl
ListView lv = (ListView)findViewById(android.R.id.list);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
}
});