Android - ClassCastException?

时间:2011-04-19 09:13:52

标签: android sharedpreferences classcastexception getstring

@Override
public void onPause()
{
    super.onPause();
    save(notes.itemSelected);
}
@Override
public void onResume()
{
    super.onResume();
    notes.itemSelected.clear();
    notes.itemSelected = load();

}
@Override
public void onRestart()
{
    super.onRestart();
    notes.itemSelected.clear();
    notes.itemSelected = load();

}

private void save(final ArrayList<String> isChecked) {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
 for(Integer i = 0; i < isChecked.size(); i++)
 {
     editor.putString(i.toString(), isChecked.get(i));
 }
editor.commit();
}

private ArrayList<String> load() {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
    ArrayList<String> reChecked = new ArrayList<String>();
    for(Integer i = 0; i < notes.getCount(); i++)
    {
        String s= i.toString();
        Log.e(TAG, s);
        reChecked.add(i, sharedPreferences.getString(s, "empty"));
    }
    return reChecked;
}

这是我的自定义适配器,其实例名为我在上面的代码中使用的注释。

public class IconAdapter extends BaseAdapter
{
private Activity activity;
private Object[] data;
private ArrayList<HashMap<String,String>> listItems;
public static LayoutInflater inflater = null;
private PackageManager pm;
public ArrayList<Boolean> itemChecked = new ArrayList<Boolean>();
public ArrayList<String> itemSelected = new ArrayList<String>();
public ArrayList<CheckBox> ctv = new ArrayList<CheckBox>();
//TextView textView;
CheckBox cb;
//ImageView imageView;
public CompleteTaskManager ctm = new CompleteTaskManager();

public IconAdapter(Activity a, ArrayList<HashMap<String,String>> items)
{
    activity = a;
    listItems = items;
    data = items.toArray();
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    pm = a.getPackageManager();


    for(int i = 0; i < items.size(); i++)
    {
        itemChecked.add(i,false);
    }


    for(int i = 0; i < items.size(); i++)
    {
        itemSelected.add(i, " ");
    }
     for(int i = 0; i < items.size(); i++)
    {
        cb  = new CheckBox(a);
        ctv.add(i,cb);
    }
}

public int getCount() {
    return listItems.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public static class ViewHolder{
    public TextView textView;
    public ImageView imageView;
    public CheckBox checkBox;
}

public View getView(final int position, View convertView, ViewGroup parent)
{
    View row = convertView;
    final ViewHolder holder;
    if(convertView==null)
    {
        row = inflater.inflate(R.layout.item, parent, false);
        holder = new ViewHolder();
        holder.textView = (TextView)row.findViewById(R.id.text1);
        holder.checkBox = (CheckBox)row.findViewById(R.id.check);
        holder.imageView = (ImageView)row.findViewById(R.id.image);
        row.setTag(holder);
    }
    else
    {
        holder = (ViewHolder)row.getTag();

    }

    String s = data[position].toString();
    String[] tokens = s.split(",");
    String[] mToken = tokens[0].split("=");
    String taskName = mToken[1];
    holder.textView.setText(taskName);
    String[] mTokens = tokens[1].split("=");
    final String pkgName =  mTokens[1].substring(0, (mTokens[1].length() - 1));

    holder.checkBox.setTag(position);
    holder.checkBox.setChecked(itemChecked.get(position));
    /*for(int i = 0; i < itemSelected.length; i++)
    {
        if(itemSelected[i].equals(pkgName))
        {
            holder.checkBox.setChecked(true);
        }
    }*/
    ctv.set(position,holder.checkBox);
    holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
        public void onCheckedChanged(CompoundButton button, boolean b) {
            Integer posClicked = (Integer)button.getTag();
            if(b)
            {
                itemChecked.set(posClicked, b);
                itemSelected.set(posClicked, pkgName);
            }
            else
            {
                itemChecked.set(posClicked, b);
                itemSelected.set(posClicked, " ");
            }
        }
    });

   holder.checkBox.setChecked(itemChecked.get(position));

    try{
        Drawable icon =   pm.getApplicationIcon(pkgName);
        holder.imageView.setImageDrawable(icon);
    }
    catch (PackageManager.NameNotFoundException ne)
    {

    }
     row.setId(position);
     return row;
}

public boolean isChecked(int position)
{
    return itemChecked.get(position);
}
public String getPkgName(int position)
{
    return itemSelected.get(position);
}
public void removeItem(int position)
{
    listItems.remove(position);
}

}

有人可以告诉我为什么我在reChecked.add(i, sharedPreferences.getString(s, "empty"));得到一个ClassCastException。 我的意思是奇怪的是它告诉我我将boolean转换为字符串但是如何?我甚至没有在这段代码中的任何地方使用布尔值。

请注意。 - notes.itemSelected是一个字符串类型的ArrayList。

请帮忙。

3 个答案:

答案 0 :(得分:3)

如果未正确输入数据类型,则会出现ClassCastException。我已更正了您的保存方法:

private void save(final ArrayList<String> isChecked) {
    SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    for(Integer i = 0; i < isChecked.size(); i++)
    {
        editor.putString(i+"", isChecked.get(i));
    }
    editor.commit();
}

同样,您还应该更改加载方法中的代码...

答案 1 :(得分:2)

实际上我弄明白为什么我得到ClassCastException。首先,我们认为不是MAC OSX是这个例外的原因。我刚刚发现了为什么会发生这种异常。

让我解释一下发生了什么。在存储ArrayList String类型之前,我使用相同的ArrayList循环保存了Boolean类型的for(),这意味着名称为i.toString的密钥()已经被占用,我在save()中调用我的onPause()方法,因为我无法在设备或模拟器上运行我的应用程序,因为这个异常所以保存的密钥永远不会被覆盖。当我想出来时,我只是将密钥名称更改为j+"0"(只是一个随机的名称)并且它已经停止抛出异常。

P.S。由于密钥已经使用,因此存在异常。

我希望它能帮助将来面临或可能面临同样情况的其他人。

@Roflcoptr和@Durga非常感谢你们的帮助,非常感谢。

感谢。

答案 2 :(得分:1)

原因必须是您的共享首选项与该键不是字符串。

documentation表示在这种情况下抛出ClassCastException: