我有 1 个名为 spinner1 的微调器。可见性设置为不可见。然后我使用图像按钮执行单击以启动微调器。当我从 strings.xml 提供数组数据时。它调用 onItemSelected。(静态数组)但是当我使用 arrayadapter 到微调器的 setadapter 时。它不会触发 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>
@Hiral 应用程序强制关闭,我得到了这个......
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)