我得到一個錯誤,即ArrayAdapter沒有資源ID,但是資源ID在這裏使用,我在活動中使用類似的代碼沒有問題,但是這是在一個分段。是有什麼我應該做的不同,因爲它是在一個片段?這就是爲什麼它不起作用?arrayadapter爲微調不工作在片段,需要資源ID
我做了一個XML佈局,並根據需要放置一個textView。
關於如何使這項工作的任何想法?從片段
棧跟蹤
07-16 20:11:44.290: E/AndroidRuntime(14278): FATAL EXCEPTION: main
07-16 20:11:44.290: E/AndroidRuntime(14278): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
07-16 20:11:44.290: E/AndroidRuntime(14278): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
07-16 20:11:44.290: E/AndroidRuntime(14278): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
oncreateview方法。這種方法是所有行動
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View result=inflater.inflate(R.layout.small_tank_layout11, container, false);
activity = getActivity();
// set shared preferences variable with shared preferences object for use
prefs = activity.getSharedPreferences("smallTankPreferences", Context.MODE_PRIVATE);
textViewOne = (TextView) result.findViewById(R.id.textView1);
textViewTwo = (TextView) result.findViewById(R.id.textView2);
spinnerOne = (Spinner) result.findViewById(R.id.spinner1);
buttonOne = (Button) result.findViewById(R.id.button1);
// get stored title quesion and set text
smallTank11question = prefs.getString("smallTank11question", "");
textViewOne.setText(smallTank11question);
// get arraylist from json string
Gson gson = new Gson();
Type type = new TypeToken<ArrayList<String>>(){}.getType();
smallTank11WeatherNames = prefs.getString("smallTank11WeatherNames", "");
smallTank11WeatherNamesArray = gson.fromJson(smallTank11WeatherNames, type);
// get the weather selected indicator stored in shared prefereces
smallTank11WeatherSelected = prefs.getInt("smallTank11WeatherSelected", -1);
//put strings of weather selections into spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(activity, R.layout.small_tank_spinnerlayout_one, smallTank11WeatherNamesArray); // layout for spinner itself
dataAdapter.setDropDownViewResource(R.layout.small_tank_spinnerlayout_one); // layout for spinner dropdown items
spinnerOne.setAdapter(dataAdapter);
return result;
} // end oncreateview
編輯在這裏,我添加的代碼small_tank_spinnerlayout_one.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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
如果你想在你需要你的陣列適配器使用自定義佈局做一個「自定義」陣列適配器,而不是默認的。 – Broak