2012-12-12 46 views
5

我有一個產品列表,但是Java抱怨方法超出了65535字節的限制。我如何添加更多的單詞並克服限制?如何超過方法代碼的65535字節限制

public class ProductList extends Activity { 

    // List view 
private ListView lv; 

// Listview Adapter 
ArrayAdapter<String> adapter; 

// Search EditText 
EditText inputSearch; 

// ArrayList for Listview 
ArrayList<HashMap<String, String>> productList; 


/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_product_list); 


    String as = System.getProperty("line.separator"); 

    String products[] = { 


      // I want these words to keep in a database 
      // Because here is 65kb limit but I need more... 



      "Banana" +as+ "Its color is yellow." , 
      "Orange" +as+ "Orange is a sour fruit." , 
      "Onion" +as+ "Onion usually used on Pizza" , 
      "Google" +as+ "Google is a giant search engine." , 
      "Love" +as+ "Love is related to heart." , 
      "Lily" +as+ "It is one kind of white flower." , 
      "Banana" +as+ "Its color is yellow." , 
      "Orange" +as+ "Orange is a sour fruit." , 
      "Onion" +as+ "Onion usually used on Pizza" , 
      "Google" +as+ "Google is a giant search engine." , 
      "Love" +as+ "Love is related to heart." , 
      "Lily" +as+ "It is one kind of white flower." ,   
      "Banana" +as+ "Its color is yellow." , 
      "Orange" +as+ "Orange is a sour fruit." , 
      "Onion" +as+ "Onion usually used on Pizza" , 
      "Google" +as+ "Google is a giant search engine." , 







    }; 



    lv = (ListView) findViewById(R.id.list_view); 
    inputSearch = (EditText) findViewById(R.id.inputSearch); 

    // Adding items to listview 
    adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.p_list, products); 
    lv.setAdapter(adapter); 

    /** 
    * Enabling Search Filter 
    * */ 
    inputSearch.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) { 
      // When user changed the Text 
      ProductList.this.adapter.getFilter().filter(cs); 
     } 

     @Override 
     public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, 
       int arg3) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void afterTextChanged(Editable arg0) { 
      // TODO Auto-generated method stub 
     } 
    }); 


} 

} 

爲了更容易理解:

enter image description here

在此先感謝

+0

從文件中讀取數據。 – dmon

回答

8

這不是做sqlite的一個限制,只要我可以告訴 - Eclipse的錯誤顯示,這是方法的問題。

我會強烈建議如果你有很多數據,你應該保持在一個單獨的資源。然後在執行時只加載資源。不需要將它烘焙到您的代碼中。如果需要,可以隨時包含一些字符串以替換平臺行分隔符。

+1

「強烈」也應加粗和加下劃線。這只是錯誤的方法:) – dmon

0

解決方案是動態地將對象分配到堆中,這實際上是無限的。

2

你正在堆棧上分配這個數組,堆棧通常有64kb的限制。

您的解決方案是使用堆。如果你改變你的代碼爲

// allocate an array on the heap 
ArrayList<String> products = new ArrayList<String>(); 
products.add("Banana" +as+ "Its color is yellow."); 
products.add("Orange" +as+ "Orange is a sour fruit."); 
products.add("Onion" +as+ "Onion usually used on Pizza"); 
// etc 

然後我敢打賭,這將工作。

然而,您最好的選擇是做Jon Skeet所說的並將所有數據存儲在資源中並在需要時加載它。由於它看起來像你使用Android,我建議使用String Arrays,這是你應該如何處理這種類型的數據。

UPDATE:

有趣。所以事實證明,這是一個Java限制。見this answer。我的理解是,類文件中的跳轉偏移量是16位,這意味着方法最多可以有65535字節大小。

所以黑客的解決方案是這一分割成更小的方法:

// allocate an array on the heap 
ArrayList<String> products = new ArrayList<String>(); 
addFirstThousand(products); 
addSecondThousand(products); 
// etc. 

private void addFirstThousand(ArrayList<String> products) { 
    products.add("Banana" +as+ "Its color is yellow."); 
    products.add("Orange" +as+ "Orange is a sour fruit."); 
    products.add("Onion" +as+ "Onion usually used on Pizza"); 
    // etc 
} 

但是,這是一個可怕想法。將這些字符串放入某種數據文件然後在運行時加載它真的會更好。如果由於某種原因,您真的反對做正確的事情並使用Google向您提供的String Array,那麼您也可以將這些字符串放在資產中的文本文件中,然後通過普通的Java BufferedReader進行讀取。

+0

由於我是Android新手,我喜歡你的風格。但不幸的是它有同樣的問題。看看https://dl.dropbox.com/u/15065300/problem-2.png – Jani

+0

@ user1898736看起來這是JVM的限制。查看更新。 –

+0

老闆,你可能很忙,但因此要求你在我的應用程序這裏添加一個小文本文件http:// www。mediafire.com/?83bol5mhgromgg0非常感謝提前 – Jani

相關問題