2016-06-17 3 views
-1

Я получаю странную ошибку, в которой андроид не позволяет мне назначать элемент из массива ArrayList, поскольку он обнаруживает его как целое это происходит только тогда, когда элемент в позиция является целым числом, как 12 вместо 12,0java.lang.Integer не может быть отнесен к java.lang.Double

Мой код

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

я делаю что-то неправильно или есть работа вокруг с помощью этого Заранее спасибо

+0

Ну, если список объявлен как '' ArrayList почему вы пытаетесь поставить целые числа в него? Java не позволит список со смешанными типами. Единственное, что я могу сделать, это добавить элементы как «Object», а затем использовать 'instanceof' для их применения при доступе к ним из списка. Другим вариантом было бы преобразование любых целых чисел в double. Это то, что вы спрашиваете? – NoChinDeluxe

+0

Будет ли 'ArrayList ' работать для вас? Кроме того, какая строка является ошибкой? – 4castle

+0

Я получаю, что данные из синтаксического анализа и ребята, работающие над этим, имеют такие данные и parse dosent, даже разрешают просто дополнительный ноль после десятичной дроби. и объявлено как то, что из полученных данных имеет тип arraylist B0rn2C0de

ответ

4

ошибка на линии. threatlevel = (double) items.get(position);

Проблема заключается в том, что она не хочет одновременно лить и автобокс. Так как Double и Integer продлить Number, вы можете использовать:

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

это работает как шарм, спасибо :) – B0rn2C0de