2012-12-12 3 views
5

제품 목록이 있지만 Java가 메소드가 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는 함께 할 수있는 제한되지 않습니다 - 이클립스 오류가 있음을 보여주고있다 메서드의 문제입니다.

나는 강력하게 일 것입니다. 많은 양의 데이터가 있다면 별도의 리소스에 보관해야합니다. 그런 다음 실행시 자원을로드하십시오. 코드에 빵을 구울 필요가 없습니다. 필요한 경우 플랫폼 행 구분 기호로 대체 할 문자열을 포함 할 수 있습니다.

+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 :

흥미 롭. 그래서 이것이 자바의 한계라고 밝혀졌습니다. 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

나는 안드로이드에 처음 왔기 때문에, 나는 ur 스타일을 좋아한다. 하지만 불행히도 같은 문제가 있습니다. 보세요 https://dl.dropbox.com/u/15065300/problem-2.png – Jani

+0

@ user1898736 JVM 한도 인 것 같습니다. 업데이트를 참조하십시오. –

+0

보스, 당신은 매우 바쁠 수도 있지만 내 애플 리케이션에 작은 텍스트 파일을 여기에 http : // www.mediafire.com/?83bol5mhgromgg0 미리 큰 감사드립니다. – Jani

관련 문제