我有一个名为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>
@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)
答案 0 :(得分:1)
我想,你不是因为你的代码这一行而得到它:
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);
然后试试!
更多参考:
编辑:
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"
/>
答案 1 :(得分:0)
您无法使用OnItemSelectedListener
方法在spinner
ImageButton
事件中执行OnClickListener
catlist.performClick()
事件1}}
答案 2 :(得分:0)
我不知道为什么以及如何。它现在正在工作。我在setadapter之前添加了一行。
spnadp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);