本文介绍了调试告诉我用setOnItemClickListener切换setOnClickListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调试时,控制台会告诉我,我应该使用 setOnItemClickListener 而不是 setOnClickListener ,因为我有一个 AdapterView .当我按下一个按钮时,就会出现此错误,并且该按钮应该将我转换为另一个活动.我尝试实现 setOnItemClick 并导入 android.widget.AdapterView.OnItemClickListener; ,但在我的代码下仍然出现红线.我在做什么错了?

When debugging, the console tells me I should use setOnItemClickListener instead of setOnClickListener because I have an AdapterView. This error comes up when I press a button and that button is supposed to transition me to another activity. I have tried to implement setOnItemClick and import android.widget.AdapterView.OnItemClickListener; and I still get red lines under my code. What am I doing wrong?

按钮操作的原始Java代码

Original Java Code for button action

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class NewLocation extends ActionBarActivity {

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_location);
        findViewById(R.id.button4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(NewLocation.this, RoomDescription.class));
            }
        });
    }

调试错误

07-11 11:37:45.281    2141-2141/com.customledsupply.ledaudit E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.customledsupply.ledaudit, PID: 2141
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.customledsupply.ledaudit/com.customledsupply.ledaudit.RoomDescription}: java.lang.RuntimeException: Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.RuntimeException: Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead
            at android.widget.AdapterView.setOnClickListener(AdapterView.java:783)
            at com.customledsupply.ledaudit.RoomDescription.onCreate(RoomDescription.java:17)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

用于按钮活动的新Java代码

New Java Code for Button Activity

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;

public class NewLocation extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_location);
        findViewById(R.id.button4).setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(NewLocation.this, RoomDescription.class));
            }
        });
    }

推荐答案

AdapterView 上设置 OnClickListener 毫无意义,因为通常它是 AdapterView (例如 ListView 的行).这就是为什么它告诉您设置 OnItemClickListener 的原因.

Setting an OnClickListener on an AdapterView doesn't make sense because generally its the children of the AdapterView that should be clicked (e.g. rows of a ListView). That's why it's telling you to set an OnItemClickListener instead.

这样做时,您的匿名内部类需要更改,因为 OnItemClickListener 没有具有签名为 void onClick(View v)的方法.您需要

When you do that, your anonymous inner class needs to change because OnItemClickListener does not have a method with the signature void onClick(View v). You need

findViewById(R.id.button4).setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // your code here
    }
});

这篇关于调试告诉我用setOnItemClickListener切换setOnClickListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 02:36