我得到一个错误,即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