0

我在填充类型时遇到了一些Spinner困难List

目标:当Button在主菜单上按下 a 时,它会执行任务 GetTownsTask,该任务会Spinner在新屏幕上填充 a,但在设置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>
4

3 回答 3

0

问题出在townList.toArray(new String[0]). 尝试使用townList.toArray(),这将确保创建的 String 数组与 townList 的大小相同。

于 2012-07-23T15:49:14.850 回答
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)

于 2012-11-19T07:51:25.923 回答
0

//像这样填充你的微调器

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

这肯定会帮助你。

谢谢 :)

于 2012-07-23T15:50:03.337 回答