我收到一个错误,即 ArrayAdapter 没有资源 ID,但是这里使用了资源 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>