簡體   English   中英

Android:填充彈出式微調器

[英]Android: populate a popup spinner

我是Android的新手,我正在努力解決這個問題:我希望有一個彈出式微調器,其中包含來自我的一個陣列的元素。
主要活動中的代碼是:

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();  

我的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>

當我運行此代碼時,我有一個致命的例外:

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

(但是如果我在沒有ArrayAdapter部分的情況下運行它,我的彈出式微調器是空的,所以當我嘗試填充微調器時問題應該是...
我查了一下,我的列表ListPointName不為空。)

有人可以解釋一下,我做錯了什么?

非常感謝

而不是這個 -

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

做這個-

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

你試圖從主布局中找到視圖,但是你的微調器在spinnerLayout中,所以

改變Spinner s = (Spinner) 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