简体   繁体   English

Android:填充弹出式微调器

[英]Android: populate a popup spinner

I'm new to Android, and I'm struggling with this problem: I want to have a popup spinner populated with elements from one of my arrays. 我是Android的新手,我正在努力解决这个问题:我希望有一个弹出式微调器,其中包含来自我的一个阵列的元素。
The code in the main activity is: 主要活动中的代码是:

AlertDialog.Builder adb2 = new AlertDialog.Builder(this);
LayoutInflater adbInflater2 = LayoutInflater.from(this);
View SpinnerLayout = adbInflater.inflate(R.layout.spinner, null);
adb2.setView(SpinnerLayout);
adb2.setTitle("Select destination");

Spinner s = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, ListPointName);
s.setAdapter(adapter);
adb2.show();  

And my spinner.xml: 我的spinner.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" >  
            <Spinner
            android:id="@+id/spinner2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

</LinearLayout>

When I run this code, I have a fatal exception: 当我运行此代码时,我有一个致命的例外:

01-05 17:45:07.273: E/AndroidRuntime(8093): FATAL EXCEPTION: main

(but if I run it without the ArrayAdapter part, I have my popup spinner empty, so the problem should be when I try to fill the spinner… (但是如果我在没有ArrayAdapter部分的情况下运行它,我的弹出式微调器是空的,所以当我尝试填充微调器时问题应该是...
I checked, and my list ListPointName is not empty.) 我查了一下,我的列表ListPointName不为空。)

Can someone explain please what am I doing wrong? 有人可以解释一下,我做错了什么?

Thanks a lot 非常感谢

Instead of this- 而不是这个 -

Spinner s = (Spinner) findViewById(R.id.spinner2);

do this- 做这个-

Spinner s = (Spinner) SpinnerLayout.findViewById(R.id.spinner2);

you are trying to find the view from your main layout, but your spinner is in spinnerLayout, So 你试图从主布局中找到视图,但是你的微调器在spinnerLayout中,所以

Change Spinner s = (Spinner) findViewById(R.id.spinner2); 改变Spinner s = (Spinner) findViewById(R.id.spinner2); to Spinner s = (Spinner) SpinnerLayout.findViewById(R.id.spinner2); to Spinner s = (Spinner) SpinnerLayout.findViewById(R.id.spinner2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM