Java 未分配给列表视图项的字符串

Java 未分配给列表视图项的字符串,java,android,android-layout,android-listview,android-listfragment,Java,Android,Android Layout,Android Listview,Android Listfragment,我正在尝试使用strings.xml文件中的一些字符串作为列表视图的项目名称,但在部署期间,我的应用程序将无法运行。我已经在互联网的许多领域寻找了相关的教程,但它们都只是显示了硬编码的字符串。有人知道我的代码出了什么问题,以及如何使用字符串而不是硬编码来修复它吗 public class FragmentLineChooserList extends android.support.v4.app.Fragment { ListView list_linechooser;

我正在尝试使用strings.xml文件中的一些字符串作为列表视图的项目名称,但在部署期间,我的应用程序将无法运行。我已经在互联网的许多领域寻找了相关的教程,但它们都只是显示了硬编码的字符串。有人知道我的代码出了什么问题,以及如何使用字符串而不是硬编码来修复它吗

    public class FragmentLineChooserList extends android.support.v4.app.Fragment {

        ListView list_linechooser;

        String[] listContent = {
                getResources().getString(R.string.item_0),
                getResources().getString(R.string.item_1),
                getResources().getString(R.string.item_2)
        };

        private boolean mTwoPane;

        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
            View v = inflater.inflate(R.layout.fragment_line_chooser_list, container, false);

            list_linechooser = (ListView)v.findViewById(R.id.list_linechooser);
        MyColoringAdapter adapter = new MyColoringAdapter(getActivity(),listContent);
        list_linechooser.setAdapter(adapter);
       );

        return v;
    }

    private class MyColoringAdapter extends ArrayAdapter<String> {
        private final Context context;
        private final String[] values;

        public MyColoringAdapter(Context context, String[] values) {
            super(context, R.layout.list_item, values);
            this.context = context;
            this.values = values;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View rowView = inflater.inflate(R.layout.list_item, parent, false);
            TextView textView = (TextView) rowView.findViewById(R.id.list_item);
            textView.setText(values[position]);
            int textColorId = R.color.white; // Default color
            switch (position) {
                case 0:
                    textColorId = R.color.red; break;
                case 1:
                    textColorId = R.color.yellow; break;
                case 2:
                    textColorId = R.color.green; break;
            }
            textView.setTextColor(getResources().getColor(textColorId));
            return rowView;
        }
    }
}
公共类FragmentLineChooserList扩展了android.support.v4.app.Fragment{
列表视图列表\u行选择器;
字符串[]listContent={
getResources().getString(R.string.item_0),
getResources().getString(R.string.item_1),
getResources().getString(R.string.item_2)
};
私有布尔值窗格;
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图v=充气机。充气(R.layout.fragment\u line\u chooser\u list,container,false);
list\u linechooser=(ListView)v.findviewbyd(R.id.list\u linechooser);
mycloringadapter=新的mycloringadapter(getActivity(),listContent);
list_linechooser.setAdapter(适配器);
);
返回v;
}
私有类MyColoringAdapter扩展了ArrayAdapter{
私人最终语境;
私有最终字符串[]值;
公共MyColoringAdapter(上下文,字符串[]值){
super(上下文、R.layout.list_项、值);
this.context=上下文;
这个值=值;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutFlater充气器=(LayoutFlater)上下文
.getSystemService(上下文布局\充气机\服务);
视图行视图=充气机。充气(R.layout.list\u项,父项,false);
TextView TextView=(TextView)rowView.findViewById(R.id.list_项);
setText(值[位置]);
int textColorId=R.color.white;//默认颜色
开关(位置){
案例0:
textColorId=R.color.red;中断;
案例1:
textColorId=R.color.yellow;中断;
案例2:
textColorId=R.color.green;中断;
}
setTextColor(getResources().getColor(textColorId));
返回行视图;
}
}
}
你喜欢吗

  String[] listContent = {
            getActivity().getResources().getString(R.string.item_0),
            getActivity().getResources().getString(R.string.item_1),
            getActivity().getResources().getString(R.string.item_2)
    };

onCreateView(…)

getActivity().getResources().getString(R.string.item_0)下
你的
适配器
类在哪里?@MD不起作用,你能告诉我你有什么问题吗?有什么错误吗?如果是的话,那就发吧。好的,非常感谢,现在可以了。问题是它不在
onCreateView(…)