我有一个错误:setOnItemClickListener无法与微调器一起使用,这是什么问题?


103

好的,这是下面的代码,我在下面粘贴了错误消息:我试图setOnItemClickListener在微调器上使用,是否可以使用?

@Override
public void onItemClick(AdapterView<?> arg0, View v, int index, long arg3) {
    if (quantity[index]=="Meter" ){
        s1="Meter";
        Toast.makeText(v.getContext(), "convert from meter",Toast.LENGTH_SHORT).show();
    } 
}

   });

convertto.setOnItemClickListener(new OnItemClickListener()
{
    @Override
    public void onItemClick(AdapterView<?> arg, View v1, int index1, long arg4)
    {
        if ((quantity[index1]=="Meter") && (s1.equalsIgnoreCase("Meter")))
        {
            Toast.makeText(v1.getContext(), " to meter",Toast.LENGTH_SHORT).show();
            var2=var1;
            lresult.setText("" + var2);
        }
    }      
});

}

}

这是错误

FATAL EXCEPTION: main
07-04 09:48:39.912: E/AndroidRuntime(694): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.a2zunitconverter.miniproject/com.a2zunitconverter.miniproject.lenght}: java.lang.RuntimeException: setOnItemClickListener cannot be used with a spinner.
07-04 09:48:39.912: E/AndroidRuntime(694):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
07-04 09:48:39.912: E/AndroidRuntime(694):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-04 09:48:39.912: E/AndroidRuntime(694):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-04 09:48:39.912: E/AndroidRuntime(694):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-04 09:48:39.912: E/AndroidRuntime(694):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-04 09:48:39.912: E/AndroidRuntime(694):  at android.os.Looper.loop(Looper.java:123)
07-04 09:48:39.912: E/AndroidRuntime(694):  at android.app.ActivityThread.main(ActivityThread.java:3683)
07-04 09:48:39.912: E/AndroidRuntime(694):  at java.lang.reflect.Method.invokeNative(Native Method)
07-04 09:48:39.912: E/AndroidRuntime(694):  at java.lang.reflect.Method.invoke(Method.java:507)
07-04 09:48:39.912: E/AndroidRuntime(694):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-04 09:48:39.912: E/AndroidRuntime(694):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-04 09:48:39.912: E/AndroidRuntime(694):  at dalvik.system.NativeStart.main(Native Method)
07-04 09:48:39.912: E/AndroidRuntime(694): Caused by: java.lang.RuntimeException: setOnItemClickListener cannot be used with a spinner.
07-04 09:48:39.912: E/AndroidRuntime(694):  at android.widget.Spinner.setOnItemClickListener(Spinner.java:102)
07-04 09:48:39.912: E/AndroidRuntime(694):  at com.a2zunitconverter.miniproject.lenght.onCreate(lenght.java:31)
07-04 09:48:39.912: E/AndroidRuntime(694):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-04 09:48:39.912: E/AndroidRuntime(694):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-04 09:48:39.912: E/AndroidRuntime(694):  ... 11 more

setOnItemClickListener不能与微调器一起使用
VenomVendor 2012年

当您将listView.setOnItemClicklistener复制到Spinner时,会发生这种情况,Spinner会按您打算的那样进行操作,这很奇怪
EngineSense 2015年

Answers:


225

请参阅您的logcat的第一行:

java.lang.RuntimeException: setOnItemClickListener cannot be used with a spinner.

setOnItemClickListener不能与一起使用Spinner。使用setOnItemSelectedListener代替。



1
@alex我明白了。但是android studio为什么在auto complete中显示setOnItemClickListener选项,这是正常行为,错误还是其他?
Dexture '16


3
如果我们需要再次选择一个项目怎么办?此方法不适用于相同的选择。
Eftekhari

53

正如@Alex所说的那样,您必须使用

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

或者,如果您使用的是ButterKnife:

@OnItemSelected(R.id.spinner)
public void onSpinnerItemSelected(int index){
    // ...
}

17

科特林:

spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
    override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
        print("onItemSelected position = $position id = $id")
    }

    override fun onNothingSelected(parent: AdapterView<*>) {

    }
}

我写它是因为从接口内联创建对象是不同的。


5

改变这个

 convertto.setOnItemClickListener(new OnItemClickListener()
   {

    @Override
    public void onItemClick(AdapterView<?> arg, View v1, int index1, long arg4)
    {
         if ((quantity[index1]=="Meter") && (s1.equalsIgnoreCase("Meter")))
        {

        Toast.makeText(v1.getContext(), " to meter",Toast.LENGTH_SHORT).show();
        var2=var1;
        lresult.setText("" + var2);
        }

    }

   });

到onItemSelectedListener,

 convertto.setOnItemSelectedListener(new OnItemSelectedListener() {

    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
         if ((quantity[index1]=="Meter") && (s1.equalsIgnoreCase("Meter")))
        {

        Toast.makeText(v1.getContext(), " to meter",Toast.LENGTH_SHORT).show();
        var2=var1;
        lresult.setText("" + var2);
        }


    }

    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }
});

看来您无法在Spinner中使用itemClickListener,

这是解释它的错误行,

07-04 09:48:39.912: E/AndroidRuntime(694): Caused by: java.lang.RuntimeException: setOnItemClickListener cannot be used with a spinner.

5

只需将您的实现形式更改setOnItemClickListenersetOnItemSelectedListener。之后,您的代码应该可以正常工作而不会出错。

Java:

convertto.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

科特林:

convertto.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {

    override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {

    }

    override fun onNothingSelected(parent: AdapterView<*>) {

  }
}

1

解决了

 spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int  
 i, long l) {

        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.