gpt4 book ai didi

android - 当我使用 setAdapter() 时,Spinner onItemSelected 不起作用

转载 作者:行者123 更新时间:2023-11-30 04:01:35 28 4
gpt4 key购买 nike

我有 1 个微调器,名为 spinner1。可见性设置为不可见。然后我使用图像按钮执行单击以启动微调器。当我从 strings.xml 提供数组数据时。它调用 onItemSelected。 (静态数组)但是当我使用 arrayadapter 来设置微调器的适配器时。它不会触发 onItemSelected 事件。

让我给你看一些代码。

final Spinner catlist = (Spinner) findViewById(R.id.spinner1);

ArrayAdapter<String> spnadp = new ArrayAdapter<String>(getApplicationContext(), R.layout.style1,android.R.id.text1,phone_numbers);

catlist.setAdapter(spnadp);
catlist.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Toast.makeText(getApplicationContext(), "bbname came is ", Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});

phone_numbers 是一个 ArrayList。

ImageButton bt1 = (ImageButton) findViewById(R.id.imageButton1);
bt1.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
catlist.performClick();
}
});

我就是这样调用它的。

所以我的问题是设置适配器有什么问题。如果我不设置适配器,它工作正常。它触发 Onitemselected()。而在其他情况下则不会。

已编辑: (style1.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingLeft="6dip"
android:textColor="@color/maroon"
android:textStyle="bold"
android:textSize="8pt"
android:paddingRight="10dip"
android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

@希拉尔应用程序关闭,我得到了这个...

09-13 14:55:29.319: E/AndroidRuntime(8380): FATAL EXCEPTION: main
09-13 14:55:29.319: E/AndroidRuntime(8380): java.lang.NullPointerException
09-13 14:55:29.319: E/AndroidRuntime(8380): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:353)
09-13 14:55:29.319: E/AndroidRuntime(8380): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
09-13 14:55:29.319: E/AndroidRuntime(8380): at android.widget.AbsListView.obtainView(AbsListView.java:1495)
09-13 14:55:29.319: E/AndroidRuntime(8380): at android.widget.ListView.measureHeightOfChildren(ListView.java:1216)
09-13 14:55:29.319: E/AndroidRuntime(8380): at android.widget.ListView.onMeasure(ListView.java:1127)
09-13 14:55:29.319: E/AndroidRuntime(8380): at android.view.View.measure(View.java:8335)
09-13 14:55:29.319: E/AndroidRuntime(8380): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
09-13 14:55:29.319: E/AndroidRuntime(8380): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
09-13 14:55:29.319: E/AndroidRuntime(8380): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
09-13 14:55:29.319: E/AndroidRuntime(8380): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
09-13 14:55:29.319: E/AndroidRuntime(8380): at android.view.View.measure(View.java:8335)
09-13 14:55:29.319: E/AndroidRuntime(8380): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
09-13 14:55:29.319: E/AndroidRuntime(8380): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
09-13 14:55:29.319: E/AndroidRuntime(8380): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
09-13 14:55:29.319: E/AndroidRuntime(8380): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
09-13 14:55:29.319: E/AndroidRuntime(8380): at android.view.View.measure(View.java:8335)
09-13 14:55:29.319: E/AndroidRuntime(8380): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
09-13 14:55:29.319: E/AndroidRuntime(8380): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
09-13 14:55:29.319: E/AndroidRuntime(8380): at android.view.View.measure(View.java:8335)
09-13 14:55:29.319: E/AndroidRuntime(8380): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
09-13 14:55:29.319: E/AndroidRuntime(8380): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
09-13 14:55:29.319: E/AndroidRuntime(8380): at android.view.View.measure(View.java:8335)
09-13 14:55:29.319: E/AndroidRuntime(8380): at android.view.ViewRoot.performTraversals(ViewRoot.java:843)
09-13 14:55:29.319: E/AndroidRuntime(8380): at android.view.ViewRoot.handleMessage(ViewRoot.java:1892)
09-13 14:55:29.319: E/AndroidRuntime(8380): at android.os.Handler.dispatchMessage(Handler.java:99)
09-13 14:55:29.319: E/AndroidRuntime(8380): at android.os.Looper.loop(Looper.java:130)
09-13 14:55:29.319: E/AndroidRuntime(8380): at android.app.ActivityThread.main(ActivityThread.java:3835)
09-13 14:55:29.319: E/AndroidRuntime(8380): at java.lang.reflect.Method.invokeNative(Native Method)
09-13 14:55:29.319: E/AndroidRuntime(8380): at java.lang.reflect.Method.invoke(Method.java:507)
09-13 14:55:29.319: E/AndroidRuntime(8380): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
09-13 14:55:29.319: E/AndroidRuntime(8380): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
09-13 14:55:29.319: E/AndroidRuntime(8380): at dalvik.system.NativeStart.main(Native Method)

最佳答案

我想,你并没有因为这行代码而得到它:

ArrayAdapter<String> spnadp = new ArrayAdapter<String>(getApplicationContext(), R.layout.style1,android.R.id.text1,phone_numbers);

这里你的第二个参数是你的布局文件..但是,你的第三个参数应该是你的布局文件中的textview的id,你在你的第二个参数中指定了。错误地你在R.id之前添加了android。文本1。它将查看您指定的 id 的 android 默认布局,但不会得到那个。

所以尝试将这一行更改为:

(默认布局):

ArrayAdapter<String> spnadp = new ArrayAdapter<String>(getApplicationContext(), android.R.id.simple_spinner_item,phone_numbers);

然后尝试!

更多引用:

Refer this

编辑:

ArrayAdapter<String> spnadp = new ArrayAdapter<String>(getApplicationContext(),R.layout.style1,phone_numbers);

并使您的 style1.xml 像:

<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingLeft="6dip"
android:textColor="@color/maroon"
android:textStyle="bold"
android:textSize="8pt"
android:paddingRight="10dip"
android:textAppearance="?android:attr/textAppearanceLarge"
/>

关于android - 当我使用 setAdapter() 时,Spinner onItemSelected 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12418252/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com