开发者

Adding an onClick directive to a ListView stops the application from working

I have a listview which I populate with data using a selfmade class. It works like a charm. I want to add a functionality for touching one of the list items. Therefor, I add a call using the onClick directive like this:

<?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>

However, as soon as I add the onClick directive seen above, the application stops to work. The setContentView() for the开发者_StackOverflow中文版 layout stops with the following stack trace:

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

I know how to get around the problem, but I am interested why this is happening. Is it not possible to define a static onClick listener for a listview? The designer in Eclipse offers this as valid functionality. Or is this a bug?

Thanks.


You need to call setOnItemClickListener instead of onclicklistener. For buttons you can use 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) {

            }
        });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜