我有一个错误:setOnItemClickListener不能与微调器一起使用,有什么问题吗?

时间:2022-09-20 18:53:01

Kindly This is my code below, and I am pasting the error messages underneath: I am trying to use setOnItemClickListener on the spinner, is it permissible?

这是我下面的代码,我粘贴下面的错误消息:我试图在转轮上使用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);
        }
    }      
});

}

}

and here are the errors

这是错误

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

4 个解决方案

#1


166  

See the first line of your logcat:

查看logcat的第一行:

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

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

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

#2


29  

As @Alex very well said, you have to use

正如@Alex所说,你必须使用。

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

        }

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

        }
    });

Or if you are using ButterKnife:

或者如果你用的是黄油刀:

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

#3


4  

Change this

改变这一切

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

    }

   });

to onItemSelectedListener,

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

    }
});

It looks like you cannot use itemClickListener in your Spinner,

看起来你不能在旋转器中使用itemClickListener,

Here is the error line which explains it,

这是解释它的误差线,

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

#4


0  

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

    }
}

I wrote it because inline creating object from the interface is different.

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

I hope it helps someone.

我希望它能帮助某人。

#1


166  

See the first line of your logcat:

查看logcat的第一行:

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

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

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

#2


29  

As @Alex very well said, you have to use

正如@Alex所说,你必须使用。

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

        }

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

        }
    });

Or if you are using ButterKnife:

或者如果你用的是黄油刀:

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

#3


4  

Change this

改变这一切

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

    }

   });

to onItemSelectedListener,

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

    }
});

It looks like you cannot use itemClickListener in your Spinner,

看起来你不能在旋转器中使用itemClickListener,

Here is the error line which explains it,

这是解释它的误差线,

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

#4


0  

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

    }
}

I wrote it because inline creating object from the interface is different.

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

I hope it helps someone.

我希望它能帮助某人。