2012-07-23 121 views
0

我在使用List類型填充Spinner時遇到一些問題。從列表中填充微調器

目的:當Button按下主菜單上,它執行其填充新的屏幕上一個Spinner任務GetTownsTask,但設置Adapter時,它打破。

MainActivity.java

private class GetTownsTask extends AsyncTask<Void, Void, List<String>> { 
    @Override 
    protected List<String> doInBackground(Void... p) { 

     // api calls 
     List<String> towns = new ArrayList<String>(); 
     // populate towns 
     return towns; 
    } 

    @Override 
    protected void onPostExecute(List<String> townList) { 

     Spinner spinner = (Spinner) findViewById(R.id.spinner); 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(
       MainActivity.this, 
       android.R.layout.simple_spinner_dropdown_item, 
       townList.toArray(new String[0])); 
     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     spinner.setAdapter(adapter); // line 124, breaks here 
    } 
} 

崩潰日誌

FATAL EXCEPTION: main, 
java.lang.NullPointerException, 
at com.project.MainActivity$TestTask.onPostExecute(MainActivity.java:124) 

微調屏幕

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#787878"> 

    <Spinner 
     android:id="@+id/spinner" 
     android:layout_width="200dip" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/townTextView" 
     android:layout_below="@+id/townTextView" 
     android:layout_marginTop="10dp" /> 

</RelativeLayout> 
+0

返回值是'spinner'是'null'。你把它添加到你的'XML'文件中? – 2012-07-23 15:48:17

+0

'spinner'爲null,很難說出爲什麼沒有任何額外的代碼。 – dmon 2012-07-23 15:48:27

+0

您的城鎮位於頂部,但您打電話給townList.toArray()。您是否通過在onPostExecute方法中進行調試來檢查townList是否爲空? – kosa 2012-07-23 15:48:29

回答

0

的問題是機智h townList.toArray(new String[0])。嘗試使用townList.toArray(),這將確保創建的字符串數組與townList的大小相同。

+1

'''townList.toArray()'必須被鑄造成帶有此錯誤的String [] java.lang.ClassCastException:java.lang.Object []不能轉換爲java.lang.String []' – Michael 2012-07-23 15:59:22

0

//填充您的SPINNER喜歡這個

package com.javasamples; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Spinner; 
import android.widget.TextView; 

public class ArrayAdapterDemo2 extends Activity implements 
     AdapterView.OnItemSelectedListener { 
    TextView selection; 
    String[] items = { "this", "is", "a", "really", 
      "really2", "really3", 
      "really4", "really5", "silly", "list" }; 

    @Override 
    public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 
     setContentView(R.layout.main); 
     selection = (TextView) findViewById(R.id.selection); 

     Spinner spin = (Spinner) findViewById(R.id.spinner); 
     spin.setOnItemSelectedListener(this); 

     ArrayAdapter aa = new ArrayAdapter(
       this, 
       android.R.layout.simple_spinner_item, 
       items); 

     aa.setDropDownViewResource(
      android.R.layout.simple_spinner_dropdown_item); 
     spin.setAdapter(aa); 
    } 

    public void onItemSelected(AdapterView<?> parent, View v, int position, 
      long id) { 
     selection.setText(items[position]); 
    } 

    public void onNothingSelected(AdapterView<?> parent) { 
     selection.setText(""); 
    } 
}//class 

這必將幫助你。

謝謝:)

+0

你看到它不包含「townList」變量的問題,因爲他沒有提供完整的代碼。我鼓勵儘快幫助一個人 – SALMAN 2012-07-23 16:03:38

0
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
       MainActivity.this, 
       android.R.layout.simple_spinner_dropdown_item, 
       townList.toArray(new String[0])); 

看來你並不需要.toArray你可以一個List<String>對象傳遞給ArrayAdapter構造。

因此,嘗試,

ArrayAdapter<String> adapter = new ArrayAdapter<String>(
       MainActivity.this, 
       android.R.layout.simple_spinner_dropdown_item, 
       townList); 

這假定townList實際上是被填充在doInBackground()和它在被傳遞到onPostExecute(townList)