为什么我会得到ClassCastException

时间:2014-10-27 17:19:49

标签: java android eclipse listview exception-handling

我似乎遇到了ClassCastException错误。我认为这与我尝试创建独特的字符串有关,即" item" + getItemId(pos)但我不确定为什么。

我的代码是:

  @Override
     public View getView(int position, View itemLayout, ViewGroup parent) {
          final int pos = position;
          final String ITEM = "item";
          // TODO - Get the current ToDoItem
           final ToDoItem toDoItem = (ToDoItem) getItem(position);
           Log.i(TAG,"toDoItem: " + toDoItem + " getItem(position) :" + position + "View convertView: " + itemLayout);

          // TODO - Inflate the View for this ToDoItem
          // from todo_item.xml
          if (itemLayout == null) {
             LayoutInflater inflater = (LayoutInflater) mContext.getSystemService
                    (Context.LAYOUT_INFLATER_SERVICE);

             itemLayout = (RelativeLayout) inflater.inflate(R.layout.todo_item, null);
            //convertView = itemLayout;
             Log.i(TAG,"itemLayout: " + itemLayout);
         } 


        // TODO - Fill in specific ToDoItem data
        // Remember that the data that goes in this View
        // corresponds to the user interface elements defined
        // in the layout file
        final ToDoItem toDo = (ToDoItem) getItem(position);

        // TODO - Display Title in TextView
        final TextView titleView = (TextView) itemLayout.findViewById(R.id.titleView);
        titleView.setText(toDo.getTitle());

        // TODO - Set up Status CheckBox
         final CheckBox statusView = (CheckBox) itemLayout.findViewById(R.id.statusCheckBox);

         Log.i(TAG," toDo.getStatus() :" + toDo.getStatus() );

         if (getLongFromPreferences("item"+getItemId(pos) ) != -1) {
           boolean isChecked =  getBooleanFromPreferences("item"+getItemId(pos) );
           statusView.setChecked(isChecked);
         } else 
           if (toDo.getStatus() == toDo.DONE) {
           statusView.setChecked(true); 
         }  


         statusView.setOnCheckedChangeListener(new OnCheckedChangeListener() {
         @Override
         public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {

            Log.i(TAG, "Entered onCheckedChanged()");
            putLongInPreferences(getItemId(pos),"item"+getItemId(pos));
            putBooleanInPreferences(isChecked,"item"+getItemId(pos));


            // TODO - set up an OnCheckedChangeListener, which
            // is called when the user toggles the status checkbox
            //int getPos = (Integer) buttonView.getTag();
        }
    });

           // TODO - Display Priority in a TextView

           final TextView priorityView = (TextView) itemLayout.findViewById(R.id.priorityView);
           priorityView.setText(toDo.getPriority().toString());

          // TODO - Display Time and Date.
          // Hint - use ToDoItem.FORMAT.format(toDoItem.getDate()) to get date and
          // time String

          final TextView dateView = (TextView) itemLayout.findViewById(R.id.dateView);
          dateView.setText(ToDoItem.FORMAT.format(toDoItem.getDate()));

          // Return the View you just created
          return itemLayout;
}


public void putBooleanInPreferences(boolean isChecked,String key){
    SharedPreferences sharedPreferences = mContext.getSharedPreferences(TAG,Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean(key, isChecked);
    editor.commit();        
}
public boolean getBooleanFromPreferences(String key){
    SharedPreferences sharedPreferences = mContext.getSharedPreferences(TAG,Context.MODE_PRIVATE);
    Boolean isChecked = sharedPreferences.getBoolean(key, false);
    return isChecked;       
}
public void putStringInPreferences(String isString,String key){
    SharedPreferences sharedPreferences = mContext.getSharedPreferences(TAG,Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(key, isString);
    editor.commit();        
}
public String getStringFromPreferences(String key){
    SharedPreferences sharedPreferences = mContext.getSharedPreferences(TAG,Context.MODE_PRIVATE);
    String isString = sharedPreferences.getString(key, "aString");
    return isString;       
}
public void putLongInPreferences(long isString,String key){
    SharedPreferences sharedPreferences = mContext.getSharedPreferences(TAG,Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putLong(key, isString);
    editor.commit();        
}
public long getLongFromPreferences(String key){
    SharedPreferences sharedPreferences = mContext.getSharedPreferences(TAG,Context.MODE_PRIVATE);
    long isString = sharedPreferences.getLong(key, -1);
    return isString;       
}

ToDoItem如下:

public class ToDoItem {

public static final String ITEM_SEP = System.getProperty("line.separator");

public enum Priority {
    LOW, MED, HIGH
};

public enum Status {
    NOTDONE, DONE
};

public final static String TITLE = "title";
public final static String PRIORITY = "priority";
public final static String STATUS = "status";
public final static String DATE = "date";
public final static String FILENAME = "filename";

public final static SimpleDateFormat FORMAT = new SimpleDateFormat(
        "yyyy-MM-dd HH:mm:ss", Locale.US);

private String mTitle = new String();
private Priority mPriority = Priority.LOW;
private Status mStatus = Status.NOTDONE;
private Date mDate = new Date();
public Status DONE = Status.DONE;

ToDoItem(String title, Priority priority, Status status, Date date) {
    this.mTitle = title;
    this.mPriority = priority;
    this.mStatus = status;
    this.mDate = date;
}

// Create a new ToDoItem from data packaged in an Intent

ToDoItem(Intent intent) {

    mTitle = intent.getStringExtra(ToDoItem.TITLE);
    mPriority = Priority.valueOf(intent.getStringExtra(ToDoItem.PRIORITY));
    mStatus = Status.valueOf(intent.getStringExtra(ToDoItem.STATUS));

    try {
        mDate = ToDoItem.FORMAT.parse(intent.getStringExtra(ToDoItem.DATE));
    } catch (ParseException e) {
        mDate = new Date();
    }
}

public String getTitle() {
    return mTitle;
}

public void setTitle(String title) {
    mTitle = title;
}

public Priority getPriority() {
    return mPriority;
}

public void setPriority(Priority priority) {
    mPriority = priority;
}

public Status getStatus() {
    return mStatus;
}

public void setStatus(Status status) {
    mStatus = status;
}

public Date getDate() {
    return mDate;
}

public void setDate(Date date) {
    mDate = date;
}

// Take a set of String data values and 
// package them for transport in an Intent

public static void packageIntent(Intent intent, String title,
        Priority priority, Status status, String date) {

    intent.putExtra(ToDoItem.TITLE, title);
    intent.putExtra(ToDoItem.PRIORITY, priority.toString());
    intent.putExtra(ToDoItem.STATUS, status.toString());
    intent.putExtra(ToDoItem.DATE, date);

}

public String toString() {
    return mTitle + ITEM_SEP + mPriority + ITEM_SEP + mStatus + ITEM_SEP
            + FORMAT.format(mDate);
}

public String toLog() {
    return "Title:" + mTitle + ITEM_SEP + "Priority:" + mPriority
            + ITEM_SEP + "Status:" + mStatus + ITEM_SEP + "Date:"
            + FORMAT.format(mDate);
}

}

我的 LogCat 是:

10-27 17:20:46.944: E/AndroidRuntime(2734): FATAL EXCEPTION: main
10-27 17:20:46.944: E/AndroidRuntime(2734): java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.Long
10-27 17:20:46.944: E/AndroidRuntime(2734):     at android.app.SharedPreferencesImpl.getLong(SharedPreferencesImpl.java:247)
10-27 17:20:46.944: E/AndroidRuntime(2734):     at course.labs.todomanager.ToDoListAdapter.getLongFromPreferences(ToDoListAdapter.java:202)
10-27 17:20:46.944: E/AndroidRuntime(2734):     at course.labs.todomanager.ToDoListAdapter.getView(ToDoListAdapter.java:127)
10-27 17:20:46.944: E/AndroidRuntime(2734):     at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:220)
10-27 17:20:46.944: E/AndroidRuntime(2734):     at android.widget.AbsListView.obtainView(AbsListView.java:2177)
10-27 17:20:46.944: E/AndroidRuntime(2734):     at android.widget.ListView.makeAndAddView(ListView.java:1840)
10-27 17:20:46.944: E/AndroidRuntime(2734):     at android.widget.ListView.fillDown(ListView.java:675)
10-27 17:20:46.944: E/AndroidRuntime(2734):     at android.widget.ListView.fillFromTop(ListView.java:736)
10-27 17:20:46.944: E/AndroidRuntime(2734):     at android.widget.ListView.layoutChildren(ListView.java:1655)
10-27 17:20:46.944: E/AndroidRuntime(2734):     at android.widget.AbsListView.onLayout(AbsListView.java:2012)
10-27 17:20:46.944: E/AndroidRuntime(2734):     at android.view.View.layout(View.java:14289)
10-27 17:20:46.944: E/AndroidRuntime(2734):     at android.view.ViewGroup.layout(ViewGroup.java:4562)
10-27 17:20:46.944: E/AndroidRuntime(2734):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
10-27 17:20:46.944: E/AndroidRuntime(2734):     at android.view.View.layout(View.java:14289)
10-27 17:20:46.944: E/AndroidRuntime(2734):     at android.view.ViewGroup.layout(ViewGroup.java:4562)
10-27 17:20:46.944: E/AndroidRuntime(2734):     at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:349)
10-27 17:20:46.944: E/AndroidRuntime(2734):     at android.view.View.layout(View.java:14289)
10-27 17:20:46.944: E/AndroidRuntime(2734):     at android.view.ViewGroup.layout(ViewGroup.java:4562)
10-27 17:20:46.944: E/AndroidRuntime(2734):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
10-27 17:20:46.944: E/AndroidRuntime(2734):     at android.view.View.layout(View.java:14289)
10-27 17:20:46.944: E/AndroidRuntime(2734):     at android.view.ViewGroup.layout(ViewGroup.java:4562)
10-27 17:20:46.944: E/AndroidRuntime(2734):     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
10-27 17:20:46.944: E/AndroidRuntime(2734):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
10-27 17:20:46.944: E/AndroidRuntime(2734):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
10-27 17:20:46.944: E/AndroidRuntime(2734):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
10-27 17:20:46.944: E/AndroidRuntime(2734):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
10-27 17:20:46.944: E/AndroidRuntime(2734):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
10-27 17:20:46.944: E/AndroidRuntime(2734):     at android.view.Choreographer.doFrame(Choreographer.java:532)
10-27 17:20:46.944: E/AndroidRuntime(2734):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
10-27 17:20:46.944: E/AndroidRuntime(2734):     at android.os.Handler.handleCallback(Handler.java:730)
10-27 17:20:46.944: E/AndroidRuntime(2734):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-27 17:20:46.944: E/AndroidRuntime(2734):     at android.os.Looper.loop(Looper.java:137)
10-27 17:20:46.944: E/AndroidRuntime(2734):     at android.app.ActivityThread.main(ActivityThread.java:5103)
10-27 17:20:46.944: E/AndroidRuntime(2734):     at java.lang.reflect.Method.invokeNative(Native Method)
10-27 17:20:46.944: E/AndroidRuntime(2734):     at java.lang.reflect.Method.invoke(Method.java:525)
10-27 17:20:46.944: E/AndroidRuntime(2734):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-27 17:20:46.944: E/AndroidRuntime(2734):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-27 17:20:46.944: E/AndroidRuntime(2734):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

我的建议是"itemLong"+getItemId(pos)使用Long布尔使用"itemBoolean"+getItemId(pos)

问题可能是由于您的'key'字符串造成的。你正在使用

 "item"+getItemId(pos)

对于boolean和long(不知怎的getItemId(pos)对于BooleanLong变得相同