ListView项目不会更改文本颜色

时间:2015-04-22 16:04:32

标签: java android

我使用着色适配器来更改列表视图项的文本颜色,但在调试应用程序崩溃后,我收到一个错误,我不知道如何修复。我知道问题出在第77行,但原因并不清楚。关于如何纠正这个问题的任何想法?

    package com.apptacularapps.exitsexpertlondonlite;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;


public class StationChooserActivity extends ActionBarActivity {

    ListView list_linechooser;

    String[] listContent = {
            "Bakerloo line", "Central line", "Circle line",
            "District line", "Hammersmith & City line",
            "Jubilee line", "Metropolitan line",
            "Northern line", "Piccadilly line",
            "Victoria line", "Waterloo & City line",
            "Docklands Light Railway", "London Overground",
            "Tramlink"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stationchooser);


        list_linechooser = (ListView)findViewById(R.id.list_linechooser);
        MyColoringAdapter adapter = new MyColoringAdapter(this,listContent);
        list_linechooser.setAdapter(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) {
            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.bakerloo; break;
                case 1:
                    textColorId = R.color.central; break;
                case 2:
                    textColorId = R.color.circle; break;
                case 3:
                    textColorId = R.color.district; break;
                case 4:
                    textColorId = R.color.hc; break;
                case 5:
                    textColorId = R.color.jubilee; break;
                case 6:
                    textColorId = R.color.metropolitan; break;
                case 7:
                    textColorId = R.color.white; break;
                case 8:
                    textColorId = R.color.piccadilly; break;
                case 9:
                    textColorId = R.color.victoria; break;
                case 10:
                    textColorId = R.color.wc; break;
                case 11:
                    textColorId = R.color.dlr; break;
                case 12:
                    textColorId = R.color.overground; break;
                case 13:
                    textColorId = R.color.tramlink; break;
            }
            textView.setTextColor(getResources().getColor(textColorId));
            return rowView;
        }
    }
}

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:1)

您的列表项布局文件名是list_item,但我认为您没有为textview提供正确的ID。这里

TextView textView = (TextView) rowView.findViewById(R.id.list_item);

确保xml文件中的文本视图ID为list_item(我不这么认为)。 在这种情况下,只需将其更改为正确的ID,它将有希望。

答案 1 :(得分:0)

错误是第77行的NullPointerException (请避免使用图像共享错误日志,文本更好)。那条线是:

textView.setText(values[position]);

我假设您没有将null数组传递给构造函数,因此另一个可能的NullExcepction问题出现在textView中。我会说textView为null。你能检查一下吗?