java.lang.Integer 无法转换为 java.lang.Double

java.lang.Integer cannot be cast to java.lang.Double

我收到一个奇怪的错误,其中 android 不允许我从 ArrayList 分配一个项目,因为它将它检测为整数 仅当该位置的项目是 12 而不是 12.0

的整数时才会发生这种情况

我的代码

public class CategoryAdapter extends BaseAdapter {
private final Context context;
private ArrayList<Category> categories ;
private final ArrayList<Double> items;

public CategoryAdapter(Context context, ArrayList<Double> i) {
    this.context = context;
    categories = Category.getActiveCategories();
    if(i.size()==0)
    {
        Double [] ar = new Double[]{0.0,0.0,0.0,0.0};
        items = new ArrayList<Double>(Arrays.asList(ar));
    }
    else
    {
        items = i;
    }
}

@Override
public int getCount() {
    return items.size();
}

@Override
public Double getItem(int position) {
    return items.get(position);
}

@Override
public long getItemId(int position) {
    // TODO implement own logic with ID
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView;
    if (convertView == null) {
        rowView = inflater.inflate(R.layout.item_ais_category_list, parent, false);
    }
    else {
        rowView = convertView;
    }

    TextView category = (TextView) rowView.findViewById(R.id.tv_ais_category_item);
    TextView tvThreatLevel = (TextView) rowView.findViewById(R.id.tv_ais_threat_level_item);
    View circle = rowView.findViewById(R.id.v_ais_circle);

    category.setText(categories.get(position).name);
    double threatlevel = 0;
    try {
        threatlevel = (double) items.get(position);
    }
    catch (Exception e)
    {
        e.printStackTrace();
        Log.d("array",items.toString());
        Log.d("count",Integer.toString(position));
    }
    if(threatlevel<=10d) {
        tvThreatLevel.setText("Safe");
        tvThreatLevel.setTextColor(context.getResources().getColor(R.color.alert_low));
    }
    else if(threatlevel>10d&&threatlevel<=20d) {
        tvThreatLevel.setText("Low");
        tvThreatLevel.setTextColor(context.getResources().getColor(R.color.alert_low));
    }
    else if(threatlevel >20d &&threatlevel<=40d)
    {
        tvThreatLevel.setText("Medium");
        tvThreatLevel.setTextColor(context.getResources().getColor(R.color.alert_med));
    }
    else
    {
        tvThreatLevel.setText("High");
        tvThreatLevel.setTextColor(context.getResources().getColor(R.color.alert_high));
    }
    GradientDrawable bg = (GradientDrawable)circle.getBackground();
    int color = Color.parseColor(categories.get(position).color);
    bg.setColor(color);
    return rowView;
}
}

这是错误的堆栈跟踪

                06-18 00:52:24.324 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double
            06-18 00:52:24.325 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at com.flagaspot.flagger.Utilities.CategoryAdapter$override.getView(CategoryAdapter.java:76)
            06-18 00:52:24.325 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at com.flagaspot.flagger.Utilities.CategoryAdapter$override.access$dispatch(CategoryAdapter.java)
            06-18 00:52:24.326 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at com.flagaspot.flagger.Utilities.CategoryAdapter.getView(CategoryAdapter.java:0)
            06-18 00:52:24.326 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.widget.AbsListView.obtainView(AbsListView.java:2346)
            06-18 00:52:24.326 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.widget.ListView.makeAndAddView(ListView.java:1876)
            06-18 00:52:24.326 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.widget.ListView.fillDown(ListView.java:702)
            06-18 00:52:24.326 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.widget.ListView.fillFromTop(ListView.java:763)
            06-18 00:52:24.326 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.widget.ListView.layoutChildren(ListView.java:1685)
            06-18 00:52:24.326 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.widget.AbsListView.onLayout(AbsListView.java:2148)
            06-18 00:52:24.326 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.view.View.layout(View.java:16639)
            06-18 00:52:24.326 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.view.ViewGroup.layout(ViewGroup.java:5437)
            06-18 00:52:24.326 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
            06-18 00:52:24.326 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
            06-18 00:52:24.326 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
            06-18 00:52:24.326 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.view.View.layout(View.java:16639)
            06-18 00:52:24.326 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.view.ViewGroup.layout(ViewGroup.java:5437)
            06-18 00:52:24.326 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
            06-18 00:52:24.327 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
            06-18 00:52:24.327 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.view.View.layout(View.java:16639)
            06-18 00:52:24.327 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.view.ViewGroup.layout(ViewGroup.java:5437)
            06-18 00:52:24.327 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
            06-18 00:52:24.327 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
            06-18 00:52:24.327 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.view.View.layout(View.java:16639)
            06-18 00:52:24.327 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.view.ViewGroup.layout(ViewGroup.java:5437)
            06-18 00:52:24.327 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
            06-18 00:52:24.327 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
            06-18 00:52:24.327 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
            06-18 00:52:24.327 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.view.View.layout(View.java:16639)
            06-18 00:52:24.327 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.view.ViewGroup.layout(ViewGroup.java:5437)
            06-18 00:52:24.327 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1187)
            06-18 00:52:24.328 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.view.View.layout(View.java:16639)
            06-18 00:52:24.328 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.view.ViewGroup.layout(ViewGroup.java:5437)
            06-18 00:52:24.328 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
            06-18 00:52:24.328 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
            06-18 00:52:24.328 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.view.View.layout(View.java:16639)
            06-18 00:52:24.328 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.view.ViewGroup.layout(ViewGroup.java:5437)
            06-18 00:52:24.328 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
            06-18 00:52:24.328 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
            06-18 00:52:24.328 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
            06-18 00:52:24.328 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.view.View.layout(View.java:16639)
            06-18 00:52:24.328 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.view.ViewGroup.layout(ViewGroup.java:5437)
            06-18 00:52:24.328 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
            06-18 00:52:24.328 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
            06-18 00:52:24.328 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.view.View.layout(View.java:16639)
            06-18 00:52:24.328 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.view.ViewGroup.layout(ViewGroup.java:5437)
            06-18 00:52:24.328 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
            06-18 00:52:24.329 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
            06-18 00:52:24.329 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
            06-18 00:52:24.329 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.view.View.layout(View.java:16639)
            06-18 00:52:24.329 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.view.ViewGroup.layout(ViewGroup.java:5437)
            06-18 00:52:24.329 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
            06-18 00:52:24.329 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
            06-18 00:52:24.329 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2934)
            06-18 00:52:24.329 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.view.View.layout(View.java:16639)
            06-18 00:52:24.329 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.view.ViewGroup.layout(ViewGroup.java:5437)
            06-18 00:52:24.329 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)
            06-18 00:52:24.329 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)
            06-18 00:52:24.329 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
            06-18 00:52:24.329 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
            06-18 00:52:24.329 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
            06-18 00:52:24.329 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.view.Choreographer.doCallbacks(Choreographer.java:670)
            06-18 00:52:24.330 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.view.Choreographer.doFrame(Choreographer.java:606)
            06-18 00:52:24.330 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
            06-18 00:52:24.330 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
            06-18 00:52:24.330 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
            06-18 00:52:24.331 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.os.Looper.loop(Looper.java:148)
            06-18 00:52:24.331 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5456)
            06-18 00:52:24.331 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
            06-18 00:52:24.331 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            06-18 00:52:24.331 14845-14845/com.example.pulkitjuneja.flag_a_spot W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616

例如,如果数组是

[251.03, 1086.97, 891.53, 615]

错误发生在位置 3

我是不是做错了什么,或者有什么解决方法吗? 提前致谢

the error is on line. threatlevel = (double) items.get(position);

问题似乎是它不想同时投射和自动装箱。由于 DoubleInteger 都扩展了 Number,您可以使用:

threatlevel = items.get(position).doubleValue();