2013-09-29 63 views
0

我在點擊列表項時遇到ClasscastException。我想通過點擊listitem移動到詳細信息屏幕。Android listitem點擊給予ClasscastException

Here is my code:  

private MyCustomAdapter dataAdapter = null; 
    private View currentView = null; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.manager_leave_requests_list); 
     displayListView(); 
    } 

    private void displayListView() 
    { 
     ArrayList<MLeaveRequests> lrList = new ArrayList<MLeaveRequests>(); 

     MLeaveRequests requests = new MLeaveRequests("12345678", "Shyam", 
       "09/29/2013", "Earned Leave", 10, "08/28/2013", "08/28/2013", 1, "XYZ"); 
     lrList.add(requests); 
     requests = new MLeaveRequests("22345678", "Shyam", "09/29/2013", 
       "Earned Leave", 10, "08/28/2013", "08/28/2013", 1, "XYZ"); 
     lrList.add(requests); 
     requests = new MLeaveRequests("32345678", "Shyam", "09/29/2013", 
       "Earned Leave", 10, "08/28/2013", "08/28/2013", 1, "XYZ"); 
     lrList.add(requests); 
     requests = new MLeaveRequests("42345678", "Shyam", "09/29/2013", 
       "Earned Leave", 10, "08/28/2013", "08/28/2013", 1, "XYZ"); 
     lrList.add(requests); 
     requests = new MLeaveRequests("52345678", "Shyam", "09/29/2013", 
       "Earned Leave", 10, "08/28/2013", "08/28/2013", 1, "XYZ"); 
     lrList.add(requests); 
     requests = new MLeaveRequests("62345678", "Shyam", "09/29/2013", 
       "Earned Leave", 10, "08/28/2013", "08/28/2013", 1, "XYZ"); 
     lrList.add(requests); 
     requests = new MLeaveRequests("72345678", "Shyam", "09/29/2013", 
       "Earned Leave", 10, "08/28/2013", "08/28/2013", 1, "XYZ"); 
     lrList.add(requests); 

     //create an ArrayAdaptar 
      dataAdapter = new MyCustomAdapter(this, R.layout.manager_leave_requests_list_item, lrList); 

     ListView list = (ListView) findViewById(R.id.mngrLeaveRequestsList) ; 
     list.setAdapter(dataAdapter); 

     list.setOnItemClickListener(new OnItemClickListener() { 
       public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 

       currentView = view; 

       //get reference to the MLeaveRequests Object 

       MLeaveRequests requests = (MLeaveRequests) view.getTag(); 

       Intent reqDetails = new Intent(ManagerLeaveRequestsList.this,LeaveRequestDetails.class); 
       Bundle b = new Bundle(); 
       //pass the country object as a parcel 
       b.putParcelable("Request", requests); 
       reqDetails.putExtras(b); 
       startActivity(reqDetails); 

       } 
       }); 
    } 
    private class MyCustomAdapter extends ArrayAdapter<MLeaveRequests>{ 
     private ArrayList<MLeaveRequests> leaves; 


     public MyCustomAdapter(
       Context context, 
       int managerLeaveRequestsListItem, 
       ArrayList<MLeaveRequests> lrList) { 
      // TODO Auto-generated constructor stub 
      super(context, managerLeaveRequestsListItem, lrList); 
      this.leaves = lrList; 

     } 
     public class ViewHolder{ 
      TextView requestID = null; 
      TextView appliedBy = null; 
      TextView appliedOn = null; 
     } 
     @Override 
      public View getView(int position, View convertView, ViewGroup parent) 
      { 
      ViewHolder holder; 

      if(convertView == null) 
      { 
      LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = li.inflate(R.layout.manager_leave_requests_list_item, null); 
      holder = new ViewHolder(); 
      holder.requestID = (TextView) convertView.findViewById(R.id.txtMLRID); 
      holder.appliedBy = (TextView) convertView.findViewById(R.id.txtMLRAppliedBy); 
      holder.appliedOn = (TextView) convertView.findViewById(R.id.txtMLRAppliedOn); 
      convertView.setTag(holder); 
      } 
      else 
       holder = (ViewHolder) convertView.getTag(); 

      final MLeaveRequests mlr = leaves.get(position); 
      if(mlr != null){ 
       mlr.setListPosition(position); 
       holder.requestID.setText(mlr.getRequestID()); 
       holder.appliedBy.setText(mlr.getAppliedBy()); 
       holder.appliedOn.setText(mlr.getAppliedOn()); 

      } 

      return convertView; 
      } 

    } 

下面是從logcat中的條目:

09-29 15:49:10.942: ERROR/AndroidRuntime(487): FATAL EXCEPTION: main 
09-29 15:49:10.942: ERROR/AndroidRuntime(487): java.lang.ClassCastException: com.XXXXXX.XXXXXXXXXX.ManagerLeaveRequestsList$MyCustomAdapter$ViewHolder 
09-29 15:49:10.942: ERROR/AndroidRuntime(487):  at com.XXXXXXXX.XXXXXXXXXX.ManagerLeaveRequestsList$1.onItemClick(ManagerLeaveRequestsList.java:75) 
09-29 15:49:10.942: ERROR/AndroidRuntime(487):  at android.widget.AdapterView.performItemClick(AdapterView.java:284) 
09-29 15:49:10.942: ERROR/AndroidRuntime(487):  at android.widget.ListView.performItemClick(ListView.java:3382) 
09-29 15:49:10.942: ERROR/AndroidRuntime(487):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696) 
09-29 15:49:10.942: ERROR/AndroidRuntime(487):  at android.os.Handler.handleCallback(Handler.java:587) 
09-29 15:49:10.942: ERROR/AndroidRuntime(487):  at android.os.Handler.dispatchMessage(Handler.java:92) 
09-29 15:49:10.942: ERROR/AndroidRuntime(487):  at android.os.Looper.loop(Looper.java:123) 
09-29 15:49:10.942: ERROR/AndroidRuntime(487):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
09-29 15:49:10.942: ERROR/AndroidRuntime(487):  at java.lang.reflect.Method.invokeNative(Native Method) 
09-29 15:49:10.942: ERROR/AndroidRuntime(487):  at java.lang.reflect.Method.invoke(Method.java:521) 
09-29 15:49:10.942: ERROR/AndroidRuntime(487):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
09-29 15:49:10.942: ERROR/AndroidRuntime(487):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
09-29 15:49:10.942: ERROR/AndroidRuntime(487):  at dalvik.system.NativeStart.main(Native Method) 

不知道爲什麼我收到這個錯誤。請幫忙。

回答

0

在你getView功能convertView.setTag(mlr);return convertView; 和它發生,因爲你正在使用ViewHolder對象setTag它鑄造於MLeaveRequests對象上getTag

之前-------------- ---------------------------------編輯---------------- -------------------------------

public View getView(int position, View convertView, ViewGroup parent) 
{ 
    ViewHolder holder; 

    LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    convertView = li.inflate(R.layout.manager_leave_requests_list_item, null); 
    holder = new ViewHolder(); 
    holder.requestID = (TextView) convertView.findViewById(R.id.txtMLRID); 
    holder.appliedBy = (TextView) convertView.findViewById(R.id.txtMLRAppliedBy); 
    holder.appliedOn = (TextView) convertView.findViewById(R.id.txtMLRAppliedOn); 

    final MLeaveRequests mlr = leaves.get(position); 
    convertView.setTag(mlr); 
    if(mlr != null){ 
     mlr.setListPosition(position); 
     holder.requestID.setText(mlr.getRequestID()); 
     holder.appliedBy.setText(mlr.getAppliedBy()); 
     holder.appliedOn.setText(mlr.getAppliedOn()); 
    } 

    return convertView; 
} 
+0

嗨Rehan,你能告訴我如何改變碼。我是Android的初學者。 – user2740599

+0

在您的MyCustomAdapter類中,轉到函數'public View getView(int position,View convertView,ViewGroup parent)'並在'return convertView'之前寫入'convertView.setTag(mlr);' 如果您還在得到任何疑問,隨時問:) – Rehan

+1

嗨Rehan,即使在添加 convertView.setTag(mlr); return convertView; 我得到相同的錯誤。任何其他幫助。 – user2740599