Java – Too Large Code and Too Many Constant Errors (Android Studio)

Too Large Code and Too Many Constant Errors (Android Studio)… here is a solution to the problem.

Too Large Code and Too Many Constant Errors (Android Studio)

I’m having an unsolvable problem; I looked it up on the Internet and didn’t find an exact solution, or at least I don’t know very well and don’t know.

Anyway, I’ve installed Android Studio v. 2.1 and that’s what I want to do:

At first I created a SQLite database to override the

onCreate method, then in another override of the same method, I wrote a method to check if the database is empty; If true, it will add about 300,000 words.
This is the part:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Database db = new Database(getApplicationContext());
    db.open();

if (db.queryTutteParole().getCount() == 0) {

db.inserisciParole("pane", "no");
        db.inserisciParole("latte", "no");
        db.inserisciParole("uovo", "no");
        db.inserisciParole("farina", "no");
        db.inserisciParole("sale", "no");
        and all the remaining words

}

db.close();
}

To clarify: queryTutteParole() is a way to return a cursor for everything in a database, inserisciParole() puts words and their values into the database

Well, there are no errors in the code (I only tested 5 words), but if I enter all 300000 words and try to run it on my device, I get the following error:

Here’s the image with the errors

What am I going to do?

Thanks 🙂

Solution

Create an array.xml file in your res/values folder and place your content in it as follows:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="yourArray">
        <item>pane,no</item>
        <item>latte,no</item>
        <item>uovo,no</item>
    </string-array>
</resources>

Then iterate it like this :

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

String[] yourArray = getResources().getStringArray(R.array.yourArray);

if (db.queryTutteParole().getCount() == 0) {

for (String s : yourArray) {
            String[] splittedArray = s.split(",");
            db.inserisciParole(splittedArray[0], splittedArray[1]);
        }
    }

db.close();
}

Related Problems and Solutions