我在使用Spinner
类型填充List
时遇到了一些困难。
目标:当在主菜单上按下Button
时,它会执行任务GetTownsTask,它会在新屏幕上填充Spinner
,但在设置Adapter
时会中断。< / p>
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 :(得分:0)
问题在于townList.toArray(new String[0])
。尝试使用townList.toArray()
,这将确保创建的String数组与townList的大小相同。
答案 1 :(得分: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
这肯定会帮到你。
谢谢:)
答案 2 :(得分: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)