0

可能重复:
如何超过方法代码的 65535 字节限制

我在堆中有超过 64kb 的数据,我该如何克服这个限制?

public class ProductList extends Activity {

    //Listview
    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");


                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");
                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");
                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");
                products.add("Banana" +as+ "Its color is yellow.");
                products.add("Orange" +as+ "Orange is a sour fruit.");




        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
            }
        });


    }

}

这是问题的图像https://dl.dropbox.com/u/15065300/problem-4.png

我是android新手,所以需要详细的建议。

4

2 回答 2

1

Java 将方法的大小限制为 65535 字节。你可以做的是将你的 onCreate 方法分解成更小的方法,然后在你的 onCreate 中调用这些方法。

编辑:

您可以做的是创建一个单独的方法来填充您的列表。您需要在创建列表适配器之前调用它。

注意:尽管正如其他人所提到的,如果您有这么多像这样硬编码的值,通常被认为是不好的做法。建议将其更新为从数据库查询或从某种类型的文件(txt、xml)中读取。

public class ProductList extends Activity 
{
    //Listview
    private ListView lv;

    // Listview Adapter
    ArrayAdapter<String> adapter;

    // Search EditText
    EditText inputSearch;

    ArrayList<String> products = new ArrayList<String>();

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

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

        // Adding items to listview
        initProductList();
        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
            }
        });
    }

    private void initProductList()
    {
        String as = System.getProperty("line.separator"); 
        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");
        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");
        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");
        products.add("Banana" +as+ "Its color is yellow.");
        products.add("Orange" +as+ "Orange is a sour fruit.");
    }
}
于 2012-12-13T18:01:40.043 回答
1

你没有。您需要将数据存储在SQLite 数据库中,或从文件中读取。单个方法中的代码不能超过 64kB,虽然将其拆分为多个方法也可以,但这是一种糟糕的编码实践。

强烈建议您将数据保存在 SQLite 数据库中。是一个很好的教程。

于 2012-12-13T18:45:10.140 回答