我想制作一个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)

最佳答案

我认为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;
}

10-04 19:13