-1

Android が ArrayList から項目を割り当てることを許可していないという奇妙なエラーが発生しています。これは、位置にある項目が 12.0 ではなく 12 のような整数である場合にのみ発生する整数として検出されるためです。

私のコード

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で発生します

私は何か間違ったことをしていますか、それとも回避策はありますか よろしくお願いします

4

1 に答える 1