"Missing return statement" 尽管它存在,但仍发生错误

"Missing return statement" error occurring despite its presence

我正在尝试实施异常检查器,但由于某种原因发生了错误。我不知道为什么当 return 语句明显存在时我会收到此错误。有人需要做什么才能解决这个问题吗?

错误

Missing return statement

代码

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

改变这个:

return rowView;
        }

为此:

    }
return rowView;

你的 return 在 if 里面 =) 很难判断,因为你没有正确缩进。

确保添加:

View rowView = null;

在 if 语句之前,否则您将超出范围。