Debug告诉我用setOnItemClickListener切换setOnClickListener

时间:2015-07-11 17:04:53

标签: android android-studio

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

按钮操作的原始Java代码

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代码

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));
            }
        });
    }

1 个答案:

答案 0 :(得分:2)

OnClickListener上设置AdapterView并不合理,因为通常应该点击AdapterView的子项(例如ListView的行)。这就是为什么它会告诉您设置OnItemClickListener的原因。

执行此操作时,您的匿名内部类需要更改,因为OnItemClickListener没有带签名void onClick(View v)的方法。你需要

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
    }
});