1

我有 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)
4

3 回答 3

1

我认为,您并没有仅仅因为您的这行代码而得到它:

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

在这里,您的第二个参数是您的布局文件。但是,您的第三个参数应该是布局文件中的 textview 的 id,您在第二个参数中指定。错误地您在 R.id.text1 之前添加了 android。它将查看您指定的 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" 
/>
于 2012-09-14T05:13:11.403 回答
0

您无法 使用方法 在您的 事件中执行OnItemSelectedListener事件spinnerImageButton OnClickListenercatlist.performClick()

于 2012-09-14T05:00:23.100 回答
0

我不知道为什么以及如何。它现在工作。我在 setadapter 之前添加了一行。

spnadp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
于 2012-09-14T09:40:27.093 回答