2012-12-12 27 views
5

私は製品リストを持っていますが、メソッドが65535バイトの制限を超えているとJavaがエラーを発する。どのように単語を追加して制限を克服できますか?理解を容易にするためメソッドコードの上限を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

私はアンドロイドに新しいので、私はウルスタイルが好きです。しかし、残念ながらそれは同じ問題を抱えています。どうぞご覧くださいhttps://dl.dropbox.com/u/15065300/problem-2.png – Jani

+0

@ user1898736 JVMの制限のようです。アップデートを参照してください。 –

+0

ボス、あなたは非常に忙しいかもしれないので、私のアプリでここに小さなテキストファイルを追加するように要求しているhttp:// www。mediafire.com/?83bol5mhgromgg0事前に大きな感謝 – Jani

関連する問題