A.

Ok, today I repaired some bugs of our project. I was so frustrated by a problem that if I want to import some bundles in the bundle list, it always only shows the last bundle in the list by multiply times which is the count of all bundles. Obviously there’s some bugs caused the bundle list was covered by the last one.

I debugged for a long time and I finally got the point. That is how I make the foolish mistake:

Here is part of the wrong code.

queryRawRes = new HashMap<String,String>();

    while(cursor.moveToNext()){

        for(int i = 0; i < n; i++){

            queryRawRes.put(columnName[i], cursor.getString(i));

        }

    queryRes.add(queryRawRes);

}

And this is the correct one.

while(cursor.moveToNext()){

    queryRawRes = new HashMap<String,String>();

    for(int i = 0; i < n; i++){

       queryRawRes.put(columnName[i], cursor.getString(i));

    }

    queryRes.add(queryRawRes);

}

Hum..looks really similar, right? The difference is just this sentence: “queryRawRes = new HashMap<String,String>();” which means create a new object of HashMap. However, the position is one of the most important parts for code. If I write in the former the HashMap queryRawRes which was put into the ArrayList will be continuously updated until the “for loop” stops. That means, we put the first query result(which means a raw in database) into the “queryRawRes” and put it into the “queryRes”, but we change the “queryRawRes” the next time when the second query result come into the loop. Remember, although we have put the first “queryRawRes” into the “queryRes”, it’s the same object, so the result is the first one was covered by the second one. And the second add is just add another pointer to point to the same object. As a result, the object was the last one of the query result and all the pointers of “queryRes”(it’s an arraylist) point to this HashMap. That’s why the list of bundles are all the same and was exactly the last one.

After I changing code, every query result was put into one HashMap and that’s the correct solution to this problem.

B.

Ok, the second problem I encountered is when I wanted to make a dex file for one of my bundles but the console couldn’t make it which is really strange because I do this step by following the official tuition. It said “Dx bad class file magic (cafebabe) or version (0034.0000)”.

I searched a lot of time on the stackoverflow and finally I find this is because of the edition of JRE(Java Runtime Enviroment). I must use jre1.6 or before while I was using the latest jre1.8 so that caused the crash.

C.

About GRE, I reviewed about 300 words today which make me feel my brain will explode.

I also planned to write a composition but I have no time to do this.

Leave a Reply

Your email address will not be published. Required fields are marked *