尽管存在,但仍会出现“缺少返回语句”错误

时间:2015-08-09 21:18:34

标签: java android android-view android-adapter

我正在尝试实现异常检查,但出现错误的原因。我不知道为什么我在返回语句显然出现时会收到此错误。有没有人需要做什么才能解决这个问题?

错误

  

缺少退货声明

代码

    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) {
            if(convertView == null) {

                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);
                // Set text
                textView.setText(values[position]);
                // Set color depending on position
                int textColorId = R.color.white; // Default color
                switch (position) {
                    case 0:
                        textColorId = R.color.brown; break;
                    case 1:
                        textColorId = R.color.red; break;
                    case 2:
                        textColorId = R.color.yellow; break;
                }
                textView.setTextColor(getResources().getColor(textColorId));
                return rowView;
            }
        }
    }

更新

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) {
        View rowView = convertView;
        if (convertView == null) {
            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);
            // Set text
            textView.setText(values[position]);
            // Set color depending on 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;
    }
}

1 个答案:

答案 0 :(得分:3)

改变这个:

return rowView;
        }

对此:

    }
return rowView;

你的回复在if =里面。很难分辨,因为你没有正确缩进。

请务必添加:

View rowView = null;
在if语句之前

,否则你将超出范围。