我正在尝试从String资源填充我的ListFragment,但是我遇到了一些问题。
我的应用程序因错误Caused by: java.lang.IllegalStateException: Fragment Fragment2{45f6f8d0} not attached to Activity
而崩溃,我不确定原因。
当我从类中使用本地字符串填充ListFragment但当我尝试使用“R.string.resource”
时,它是有效的如何通过将我的字符串引用到R.string.resorce来实现这一目标?
public class Fragment2 extends SherlockListFragment {
public class MyListAdapter extends ArrayAdapter<String> {
Context myContext;
public MyListAdapter(Context context, int textViewResourceId,
String[] objects) {
super(context, textViewResourceId, objects);
myContext = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// return super.getView(position, convertView, parent);
LayoutInflater inflater = (LayoutInflater) myContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.row, parent, false);
TextView label = (TextView) row.findViewById(R.id.month);
label.setText(month[position]);
ImageView icon = (ImageView) row.findViewById(R.id.icon);
icon.setImageResource(drawables[position]);
return row;
}
}
int[] drawables = { R.drawable.abstract_icon, R.drawable.animal_icon,
R.drawable.anime_icon, R.drawable.buildings_icon,
R.drawable.icon_cars, R.drawable.cartoon_icon,
R.drawable.icon_city, R.drawable.colorful_icon,
R.drawable.icon_fantasy, R.drawable.flower_iconm,
R.drawable.icon_food, R.drawable.icon_girly,
R.drawable.nature_icon, R.drawable.nightscape_icon,
R.drawable.paintings_icon, R.drawable.patters_icon,
R.drawable.portrait, R.drawable.rain_icon, R.drawable.sea_icon,
R.drawable.space_icon, R.drawable.trees_icon,
R.drawable.vibrant_icon
};
//Here
String[] month = { getString(R.string.abstarct),
getString(R.string.animal), getString(R.string.anime),
getString(R.string.buildings), getString(R.string.cars),
getString(R.string.cartoon), getString(R.string.city),
getString(R.string.colorful), getString(R.string.fantasy),
getString(R.string.flowers), getString(R.string.food),
getString(R.string.girly), getString(R.string.nature),
getString(R.string.nightscape), getString(R.string.paintings),
getString(R.string.patterns), getString(R.string.portrait),
getResources().getString(R.string.rain), getString(R.string.sea),
getString(R.string.space), getString(R.string.trees),
getString(R.string.vibrant) };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
MyListAdapter myListAdapter = new MyListAdapter(getActivity(),
R.layout.row, month);
setListAdapter(myListAdapter);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.category, container, false);
}
答案 0 :(得分:1)
在初始化Context
时,您无法访问Fragmnet
资源...
Context
资源可以从Fragment
回调的onAttach()
来访问,您将通过该回调获得Activity
参考
所以在month
onAttach()
字符串数组
private String[] month;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
month = new String[]{ getString(R.string.abstarct),
getString(R.string.animal), getString(R.string.anime),
getString(R.string.buildings), getString(R.string.cars),
getString(R.string.cartoon), getString(R.string.city),
getString(R.string.colorful), getString(R.string.fantasy),
getString(R.string.flowers), getString(R.string.food),
getString(R.string.girly), getString(R.string.nature),
getString(R.string.nightscape), getString(R.string.paintings),
getString(R.string.patterns), getString(R.string.portrait),
getResources().getString(R.string.rain), getString(R.string.sea),
getString(R.string.space), getString(R.string.trees),
getString(R.string.vibrant) };
}
答案 1 :(得分:0)
尝试将月份更改为int
数组,该数组仅包含资源ID,并使用
label.setText(myContext.getString(month[position]));