친절하게 이것은 아래 내 코드이며 아래에 오류 메시지를 붙여 넣 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
답변
logcat의 첫 번째 줄을 참조하십시오.
java.lang.RuntimeException: setOnItemClickListener cannot be used with a spinner.
setOnItemClickListener
와 함께 사용할 수 없습니다 Spinner
. setOnItemSelectedListener
대신 사용하십시오 .
답변
@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){
// ...
}
답변
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<*>) {
}
}
인터페이스에서 인라인 생성 객체가 다르기 때문에 작성했습니다.
답변
이것을 변경
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.
답변
구현 양식 setOnItemClickListener
을 setOnItemSelectedListener
. 나중에 오류없이 코드가 제대로 작동합니다.
자바:
convertto.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Kotlin :
convertto.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
}
override fun onNothingSelected(parent: AdapterView<*>) {
}
}
답변
해결사
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int
i, long l) {
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});