Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

i am getting a weird error in which android is not allowing me assign an item from a ArrayList because it detects it as integer this happens only when the item at the position is a whole number like 12 instead of 12.0

My code

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;
}
}

and this is the stack trace of the error

                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

For example if the array is

[251.03, 1086.97, 891.53, 615]

the error occurs at position 3

am i doing something wrong or is there any work around through this Thanks in advance

share|improve this question
    
Well if the list is declared as ArrayList<Double> why are you trying to put integers into it? Java won't allow a list with mixed types like that. The only way I can think to do that would be to add the items as Object and then use instanceof to cast them when accessing them from the list. Another option would be to convert any integers to double. Is that what you're asking? – NoChinDeluxe Jun 17 at 19:39
    
Would ArrayList<Number> work for you? Also, which line is the error coming from? – 4castle Jun 17 at 19:42
    
I am fetching that data from a parse and the guys working on that have the data like that and parse dosent even allow to just an extra zero after decimal . and it is declared as that coz the fetched data is of type arraylist<Double> – B0rn2C0de Jun 17 at 19:44
    
@4castle i will try that and the error is on line. threatlevel = (double) items.get(position); – B0rn2C0de Jun 17 at 19:45
up vote 4 down vote accepted

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

The issue seems to be that it doesn't want to cast and autobox at the same time. Since both Double and Integer extend Number, you can use:

threatlevel = items.get(position).doubleValue();
share|improve this answer
    
this works like a charm , thank you :) – B0rn2C0de Jun 17 at 20:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.