【问题标题】:Populating ListFragment from String resource crashes从字符串资源崩溃填充 ListFragment
【发布时间】:2014-01-23 12:19:33
【问题描述】:

我正在尝试从字符串资源填充我的 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);
 }

【问题讨论】:

    标签: android android-listview android-listfragment


    【解决方案1】:

    在初始化 Fragmnet 时,您无法访问 Context 资源...

    Context 资源可以从Fragment 访问,通过onAttach() 回调,您将获得Activity 参考

    所以在onAttach()中初始化你的month字符串数组

        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) };
        }
    

    【讨论】:

      【解决方案2】:

      尝试将月份更改为 int 数组,该数组仅包含资源 ID,并使用

      label.setText(myContext.getString(month[position]));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-06-22
        • 2012-06-05
        • 2011-05-15
        • 2013-01-09
        • 1970-01-01
        • 2010-10-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多