web-dev-qa-db-fra.com

J'ai une erreur: setOnItemClickListener ne peut pas être utilisé avec un spinner, qu'est-ce qui ne va pas?

Kindly Voici mon code ci-dessous, et je colle les messages d'erreur ci-dessous: J'essaie d'utiliser setOnItemClickListener sur la roulette, est-il permis?

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

}

}

et voici les erreurs

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
78
Salman Amjad

Voir la première ligne de votre logcat:

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

setOnItemClickListener ne peut pas être utilisé avec un Spinner. Utilisez setOnItemSelectedListener à la place.

196
Alex Lockwood

Comme @Alex l'a très bien dit, vous devez utiliser

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

        }

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

        }
    });

Ou si vous utilisez ButterKnife:

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

Kotlin:

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<*>) {

    }
}

Je l'ai écrit parce que la création d'objet en ligne à partir de l'interface est différente.

J'espère que ça aide quelqu'un.

7
MBH

Change ça

 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

    }
});

Il semble que vous ne pouvez pas utiliser onitemClickListener dans votre Spinner,

Voici la ligne d'erreur qui l'explique,

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