Error setOnItemClickListener cannot be used with a spinner

0 votes

I am trying to use setOnItemClickListener on the spinner. 

Here is my code:

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

}

}

and here is the errors i get

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

How can I resolve this error?

Jun 1, 2020 in Java by kartik
• 37,520 points
3,570 views

1 answer to this question.

0 votes

Hello @kartik,

See the first line of your logcat:

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

setOnItemClickListener cannot be used with a Spinner. Use setOnItemSelectedListener instead.

Hope this help!!

Thank You!!

answered Jun 1, 2020 by Niroj
• 82,840 points

Related Questions In Java

+3 votes
14 answers

“A Non-static method cannot be referenced from a static context” error

Hey Techies, Non-static variables are part of the objects ...READ MORE

answered Dec 10, 2020 in Java by Roshni
• 10,480 points
249,670 views
+1 vote
2 answers
0 votes
1 answer

Why “non-static method cannot be referenced from a static context”?

You can't call something that doesn't exist. ...READ MORE

answered Oct 17, 2018 in Java by sharth
• 3,370 points
1,178 views
0 votes
1 answer

Logger cannot be resolved Java(570425394) Error occured

Hey, @Boopathy, Could you please post your code ...READ MORE

answered Dec 1, 2020 in Java by Gitika
• 65,770 points
10,138 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
0 votes
1 answer

Error:No EntityManager with actual transaction available for current thread - cannot reliably process 'persist' call

Hii @kartik, If you have @Transactional // Spring Transactional class ...READ MORE

answered May 22, 2020 in Java by Niroj
• 82,840 points
49,799 views
0 votes
1 answer

How do I make a list with checkboxes in Java Swing?

Hii @kartik, Create a custom ListCellRenderer and asign it to ...READ MORE

answered May 8, 2020 in Java by Niroj
• 82,840 points
2,407 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP