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>
에 전달되고있다. 'XML' 파일에 추가 하시겠습니까? –
'spinner'는 null입니다. 왜 여분의 코드가 없는지 알기가 어렵습니다. – dmon
상단에 타운이 있지만 townList.toArray()를 호출하고 있습니다. townList가 onPostExecute 메서드 내에서 디버깅하여 null이 아닌지 확인 했습니까? – kosa