自定义微调器,显示彩色TextViews

时间:2016-04-22 15:32:04

标签: java android nullpointerexception android-spinner

我想让Spinner选择颜色,我希望它的每个元素都是一个圆形的视图,有一些颜色[颜色在样式中]

所以我想制作一个自定义微调器适配器

class CustomSpinnerAdapter extends ArrayAdapter<Integer> {
    private Context mContext;
    private Integer  [] mColors;
    public CustomSpinnerAdapter(Context context, int resource, Integer [] Colors) {
        super(context, resource,Colors);
        mContext=context;
        mColors=Colors;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }



    public View getCustomView(int position, View convertView, ViewGroup parent) {
        View row=convertView;

        LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();

        row=inflater.inflate(R.layout.color_spinner_row, parent, false);
        TextView Circle = (TextView) row.findViewById(R.id.circle);
        GradientDrawable bgShape = (GradientDrawable)Circle.getBackground();
        bgShape.setColor(mContext.getResources().getColor(mColors[position]));
        return convertView;
    }
}

并且OnCreate代码为:

Integer colors[]={R.color.Class_Color1,R.color.Class_Color2,R.color.Class_Color3,R.color.Class_Color4,R.color.Class_Color5};
Spinner Colors_Spinner= (Spinner) findViewById(R.id.Colors_Spinner);
CustomSpinnerAdapter customSpinnerAdapter = new CustomSpinnerAdapter(this, android.R.layout.simple_spinner_dropdown_item, colors);
Colors_Spinner.setAdapter(customSpinnerAdapter);

但此代码会抛出nullPointerExption

那么如何解决这个问题?

编辑,错误日志:

FATAL EXCEPTION: main Process: com.example.atefhares.finalproject_build1, PID: 7309
                               java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()' on a null object reference
                               at android.support.v7.widget.AppCompatSpinner.compatMeasureContentWidth(AppCompatSpinner.java:561)
                               at android.support.v7.widget.AppCompatSpinner.access$500(AppCompatSpinner.java:64)
                               at android.support.v7.widget.AppCompatSpinner$DropdownPopup.computeContentWidth(AppCompatSpinner.java:751)
                               at android.support.v7.widget.AppCompatSpinner$DropdownPopup.show(AppCompatSpinner.java:776)
                               at android.support.v7.widget.AppCompatSpinner.performClick(AppCompatSpinner.java:434)
                               at android.view.View$PerformClick.run(View.java:19769)
                               at android.os.Handler.handleCallback(Handler.java:739)
                               at android.os.Handler.dispatchMessage(Handler.java:95)
                               at android.os.Looper.loop(Looper.java:135)
                               at android.app.ActivityThread.main(ActivityThread.java:5289)
                               at java.lang.reflect.Method.invoke(Native Method)
                               at java.lang.reflect.Method.invoke(Method.java:372)
                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

1 个答案:

答案 0 :(得分:1)

我认为convertView == null会导致问题:

@Override
public View getDropDownView(int position, View convertView,ViewGroup parent) {
    return getCustomView(position, convertView, parent);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    return getCustomView(position, convertView, parent);
}

public View getCustomView(int position, View convertView, ViewGroup parent) {
     Textview circle;
     if (convertView == null) {
         convertView = inflater.inflate(R.layout.color_spinner_row, parent, false);
         circle = convertView.findViewById(R.id.circle);
         convertView.setTag(circle);
     } else {
         circle = (TextView) convertView.getTag();
     }
     // Do everything you want with  "circle"


     return convertView;
}