سؤال

I'm trying to populate my ListFragment from a String resource but I'm having some problems.

My app crashes with the error Caused by: java.lang.IllegalStateException: Fragment Fragment2{45f6f8d0} not attached to Activity and I'm not sure why.

It works when I populate my ListFragment with local Strings from the class but not when I try to use "R.string.resource"

How would I be able to achieve this by referencing my strings to the 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);
 }
هل كانت مفيدة؟

المحلول

You can't access the Context resources at the time of intializing Fragmnet...

Context resources can be accessed from Fragment from the onAttach() callback through which you will get Activity reference

so initialize your month String array in 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) };
    }

نصائح أخرى

Try to change month to an int array, which only contains the resource ids, and use

label.setText(myContext.getString(month[position]));
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top