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>