2012-11-06 110 views
1

我有代碼,以使這樣的錯誤java.lang.IndexOutOfBoundsException:無效指數1,大小爲0

class LoadAllProduct extends AsyncTask<String, String, String> { 

     /** 
     * Before starting background thread Show Progress Dialog 
     * */ 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(allproduct.this); 
      pDialog.setMessage("Loading product. Please wait..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(false); 
      pDialog.show(); 
     } 

     /** 
     * getting All products from url 
     * */ 
     protected String doInBackground(String... args) { 
      Cursor cursor; 

      int i=0; 
      while(cursor.moveToNext()){ 
       id_produk[i] = cursor.getInt(0); 
       String title = cursor.getString(1); 
       //String price = cursor.getString(2); 
       //String desc = cursor.getString(3); 
       //String how_to = cursor.getString(4); 
       String time = cursor.getString(5); 
       path_image[i] = cursor.getString(7); 

       Cursor cur = db.getProductLink(id_produk[i]); 
       int y = 0; 
       String link = ""; 
       while(cur.moveToNext()){ 
        if(y == 0) link = cur.getString(0); 
        else link = link + "\n" + cur.getString(0); 
        y++; 
       } 
       product_item[i] = new MenuItem(title, link, time, path_image[i]); 
       i++; 
       cur.close(); 
      } 

      cursor.close(); 
      db.close(); 

      return null; 

     } 

     /** 
     * After completing background task Dismiss the progress dialog 
     * **/ 
     protected void onPostExecute(String file_url) { 
      // updating UI from Background Thread 
      runOnUiThread(new Runnable() { 
       public void run() { 
        mainlist.setAdapter(new ListMenuAdapter(product_item)); 
       } 
      }); 
      pDialog.dismiss(); 
     } 
} 

列表視圖,現在,我想表明我的產品的詳細只需點擊選擇項目與此代碼

mainlist.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
       // getting values from selected ListItem 


       String pid = productList.get(position).get(TAG_PID); 

       // Starting new intent 
       Intent in = new Intent(getApplicationContext(),editproductActivity.class); 

       // sending pid to next activity 
       in.putExtra(TAG_PID, pid); 
       in.putExtra("phone", mPhoneNumber); 
       in.putExtra("daftar", daftar);   
       startActivityForResult(in, 100);  
      } 
     }); 
     } 

而這個日誌貓列表視圖顯示此,我認爲錯誤原因字符串PID = productList.get(位置)獲得(TAG_PID);

11-06 12:46:25.305: ERROR/AndroidRuntime(1616): FATAL EXCEPTION: main 
11-06 12:46:25.305: ERROR/AndroidRuntime(1616): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 
11-06 12:46:25.305: ERROR/AndroidRuntime(1616):  at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257) 
11-06 12:46:25.305: ERROR/AndroidRuntime(1616):  at java.util.ArrayList.get(ArrayList.java:311) 
11-06 12:46:25.305: ERROR/AndroidRuntime(1616):  at shoop3.android.edu.allproduct$4.onItemClick(allproduct.java:175) 
11-06 12:46:25.305: ERROR/AndroidRuntime(1616):  at android.widget.AdapterView.performItemClick(AdapterView.java:284) 
11-06 12:46:25.305: ERROR/AndroidRuntime(1616):  at android.widget.ListView.performItemClick(ListView.java:3382) 
11-06 12:46:25.305: ERROR/AndroidRuntime(1616):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696) 
11-06 12:46:25.305: ERROR/AndroidRuntime(1616):  at android.os.Handler.handleCallback(Handler.java:587) 
11-06 12:46:25.305: ERROR/AndroidRuntime(1616):  at android.os.Handler.dispatchMessage(Handler.java:92) 
11-06 12:46:25.305: ERROR/AndroidRuntime(1616):  at android.os.Looper.loop(Looper.java:123) 
11-06 12:46:25.305: ERROR/AndroidRuntime(1616):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
11-06 12:46:25.305: ERROR/AndroidRuntime(1616):  at java.lang.reflect.Method.invokeNative(Native Method) 
11-06 12:46:25.305: ERROR/AndroidRuntime(1616):  at java.lang.reflect.Method.invoke(Method.java:521) 
11-06 12:46:25.305: ERROR/AndroidRuntime(1616):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
11-06 12:46:25.305: ERROR/AndroidRuntime(1616):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
11-06 12:46:25.305: ERROR/AndroidRuntime(1616):  at dalvik.system.NativeStart.main(Native Method) 

你能幫我嗎?謝謝

回答

1

我認爲productList是空的在裏面添加數據,然後檢查。

if(productList.size==0){ 

    System.out.println("list size is zero..."); 
    return; 

} 
String pid = productList.get(position).get(TAG_PID); 
+0

我認爲不是,因爲我可以顯示列表產品,我只是不能顯示產品詳細信息, –

+0

檢查我更新的答案,並將其添加到onItemClick –

+0

yead,你說得對我的尺寸是零:(如何修復它?? –

0

檢查您的ProductList是否爲空。打印出它的大小。這可能是空的,因此它顯示您的索引超出範圍

0

檢查一次您的產品清單是否爲空。如果是這樣,那麼它會拋出indexoutofbound錯誤。

相關問題