2

New to android programming. Getting Following error.

04-07 14:49:05.452: ERROR/AndroidRuntime(1566): FATAL EXCEPTION: main
        java.lang.OutOfMemoryError
        at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:94)
        at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:145)
        at java.lang.StringBuilder.append(StringBuilder.java:216)
        at com.myapp.StartupActivity.onListItemClick(StartupActivity.java:87)
        at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
        at android.widget.AdapterView.performItemClick(AdapterView.java:292)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
        at android.widget.AbsListView$1.run(AbsListView.java:3168)
        at android.os.Handler.handleCallback(Handler.java:605)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4340)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
        at dalvik.system.NativeStart.main(Native Method)

Using tab layout and there are three activities for controlling tabs. I think i crossed memory heap available to my app. Can someone tell me how to destroy objects when you navigate away from tab? Error happens in following code.

String separator = "|"
String myString = "";
for(String string : Mylist)
{
    myString += myString + string + separator;
}
2
  • Your implementation creates a lot of unnecessary intermediate strings. Consider using StringBuilder or some third-party code. With Apache Commons you could use org.apache.commons.lang.StringUtils.join(MyList, separator) instead. Commented Apr 7, 2012 at 9:38
  • 1
    Using StringBulder as suggested by Qerub solve the error but application takes too much time to complete operation. So i tried `String separator = "\\|" as suggested by hotveryspicy. Now application works flawlessly. Thanks guys. Stakeoverflow really prove useful to me. Commented Apr 7, 2012 at 10:14

1 Answer 1

1

try String separator = "\\|" instead String separator = "|"

there is a problem in using pipe symbol using java or you can use Pattern.quote("|")

@Qerub It's worth noting that the double-slash is only necessary if the expression string is compiled into your java code.

One slash is needed by the Java compiler so that it stores a literal slash in the expression String object. This isn't unique to regular expressions, all Java Strings compiled in the class files work this way. If you want one slash in your String in your compiled class file, type two slashes in your Java source.

If the regular expression is passed into the Java program at runtime -- via command-line arguments, or a property value -- this first slash is not necessary.

Another slash is needed by the regular expression parser so that the pipe is treated as a literal character. The expression parser doesn't care whether the string was compiled into the class file or provided at runtime.

Source code: \\|

which compiles to...

Class file: \|

now is read by regular expression parser as...

Expression parser treats as: |

While the same we used in split() then it treated as alteration, which mean string get split by every character

Source code: "query"

"query".split("|") give result like

q
u
e
r
y
Sign up to request clarification or add additional context in comments.

4 Comments

Aren't you thinking about String#split?
Could you provide some more information on why this is the case? I'm intrigued.
@Qerub this is the information i have, I will glad if you provide more :)
I understand why | needs to be "double-escaped" when used literally in a regexp but I fail to see how this applies to the question; @Ravi Patel is joining and not splitting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.