Android中的奇怪共享首选项

时间:2014-02-22 15:20:12

标签: android sharedpreferences

我之前已经在SharedPreference工作过,而且我从未经历过这样一个奇怪的错误。我像普通人一样实现SharedPreference。但是当我尝试检索存储在其中的数据时,它给了我一个 CLASSCASTEXCEPTION !代码如下:

代码:

public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    row_item_object = array_list_object.get(position);
    holder = new ViewHolder();

    sp = PreferenceManager.getDefaultSharedPreferences(context);


    adapter_object = new AddToListAdapter(getContext(), R.layout.add_to_list_repeat_items, array_list_object);

    editor = sp.edit();

    LayoutInflater inflator = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);;

    if(convertView == null)
    {

    convertView = inflator.inflate(R.layout.add_to_list_repeat_items,null);
    holder.title = (TextView) convertView.findViewById(R.id.add_to_wants_owns_textview);
    holder.count = (TextView) convertView.findViewById(R.id.add_to_wants_owns_count);
    holder.frame = (FrameLayout) convertView.findViewById(R.id.frame);

    convertView.setTag(holder);

    }
    else

        holder = (ViewHolder) convertView.getTag();




    holder.title.setText(row_item_object.getTitle());

    System.out.println("Sample is " + row_item_object.getTitle());

    System.out.println("The count value in position 0 is " + sp.getInt("count_key_wants",0));


    /*if(position == 0)
    {

        int shared_prefs_count_value_wants = sp.getInt("count_key_wants", 0);
        row_item_object.setcount(shared_prefs_count_value_wants);
    }

    if(position == 1)
    {
        int shared_prefs_count_value_wants = sp.getInt("count_key_owns", 0);
        row_item_object.setcount(shared_prefs_count_value_wants);
    }*/


    holder.frame.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            AddToListRowItem sel_row = null;
            int pos = position;
            System.out.println("Position on frame click: " + pos);

            System.out.println("The title is " + row_item_object.getTitle());

            if( pos == 0 )
            {
                sel_row = array_list_object.get(pos);
                System.out.println("Inside AddToWants");


                    sel_row.setTitle("Added To Wants");




                    count_value_wants = count_value_wants + 1;
                    sel_row.setcount(count_value_wants);

                    editor.putInt("count_key_wants", count_value_wants);

                    editor.commit();



                    notifyDataSetChanged();
                    notifyDataSetInvalidated();





            }
            else 
            {
                //AddToListRowItem sel_row = array_list_object.get(pos);
                System.out.println("Inside AddToWants");
                sel_row = array_list_object.get(pos);
                sel_row.setTitle("Added To Owns");


                    count_value_owns = count_value_owns + 1;

                    sel_row.setcount(count_value_owns);

                    editor.putInt("count_key_owns", count_value_owns);
                    editor.commit();


                    notifyDataSetChanged();
                    notifyDataSetInvalidated();



            }

        }

    });


    return convertView;
}

我相信代码看起来像人类做的事情。该错误源自SOP行,表示为ClassCastException:java.lang.String无法强制转换为java.lang.Integer。 什么可能出错?????

为什么我收到此错误?即使没有正确实例化Shared Preference对象,它也应该抛出NullPointerException,或者它应该打印我给出的默认值为0.也不会发生。

注意:我一直在扩展ArrayAdapter的非活动类中实现此功能。

Logcat:

02-22 20:31:46.873: E/AndroidRuntime(18240): FATAL EXCEPTION: main
02-22 20:31:46.873: E/AndroidRuntime(18240): java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
02-22 20:31:46.873: E/AndroidRuntime(18240):    at android.app.SharedPreferencesImpl.getInt(SharedPreferencesImpl.java:221)
02-22 20:31:46.873: E/AndroidRuntime(18240):    at com.example.addtolistfeb_21.AddToListAdapter.getView(AddToListAdapter.java:93)
02-22 20:31:46.873: E/AndroidRuntime(18240):    at android.widget.AbsListView.obtainView(AbsListView.java:2465)
02-22 20:31:46.873: E/AndroidRuntime(18240):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1250)
02-22 20:31:46.873: E/AndroidRuntime(18240):    at android.widget.ListView.onMeasure(ListView.java:1162)
02-22 20:31:46.873: E/AndroidRuntime(18240):    at android.view.View.measure(View.java:15598)
02-22 20:31:46.873: E/AndroidRuntime(18240):    at android.widget.RelativeLayout.measureChild(RelativeLayout.java:602)
02-22 20:31:46.873: E/AndroidRuntime(18240):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:415)
02-22 20:31:46.873: E/AndroidRuntime(18240):    at android.view.View.measure(View.java:15598)
02-22 20:31:46.873: E/AndroidRuntime(18240):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5118)
02-22 20:31:46.873: E/AndroidRuntime(18240):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-22 20:31:46.873: E/AndroidRuntime(18240):    at android.view.View.measure(View.java:15598)
02-22 20:31:46.873: E/AndroidRuntime(18240):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5118)
02-22 20:31:46.873: E/AndroidRuntime(18240):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-22 20:31:46.873: E/AndroidRuntime(18240):    at android.view.View.measure(View.java:15598)
02-22 20:31:46.873: E/AndroidRuntime(18240):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5118)
02-22 20:31:46.873: E/AndroidRuntime(18240):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-22 20:31:46.873: E/AndroidRuntime(18240):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2423)
02-22 20:31:46.873: E/AndroidRuntime(18240):    at android.view.View.measure(View.java:15598)
02-22 20:31:46.873: E/AndroidRuntime(18240):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2006)
02-22 20:31:46.873: E/AndroidRuntime(18240):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1245)
02-22 20:31:46.873: E/AndroidRuntime(18240):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1420)
02-22 20:31:46.873: E/AndroidRuntime(18240):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1138)
02-22 20:31:46.873: E/AndroidRuntime(18240):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4645)
02-22 20:31:46.873: E/AndroidRuntime(18240):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
02-22 20:31:46.873: E/AndroidRuntime(18240):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
02-22 20:31:46.873: E/AndroidRuntime(18240):    at android.view.Choreographer.doFrame(Choreographer.java:525)
02-22 20:31:46.873: E/AndroidRuntime(18240):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
02-22 20:31:46.873: E/AndroidRuntime(18240):    at android.os.Handler.handleCallback(Handler.java:615)
02-22 20:31:46.873: E/AndroidRuntime(18240):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-22 20:31:46.873: E/AndroidRuntime(18240):    at android.os.Looper.loop(Looper.java:174)
02-22 20:31:46.873: E/AndroidRuntime(18240):    at android.app.ActivityThread.main(ActivityThread.java:4952)
02-22 20:31:46.873: E/AndroidRuntime(18240):    at java.lang.reflect.Method.invokeNative(Native Method)
02-22 20:31:46.873: E/AndroidRuntime(18240):    at java.lang.reflect.Method.invoke(Method.java:511)
02-22 20:31:46.873: E/AndroidRuntime(18240):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
02-22 20:31:46.873: E/AndroidRuntime(18240):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
02-22 20:31:46.873: E/AndroidRuntime(18240):    at dalvik.system.NativeStart.main(Native Method)

logcat中的第93行是SOP行。任何有关可能导致此问题的见解都将非常感激。

1 个答案:

答案 0 :(得分:0)

在共享首选项文档中,getInt

  

返回首选项值(如果存在)或defValue。抛出   如果存在不具有此名称的首选项,则为ClassCastException   一个int。

您必须在某处使用String初始化属性count_key_wants