English 中文(简体)
what s wrong with concatenation in blackberry application?
原标题:

For the life of me, I can t understand why adding a concatenated string to the MainScreen is causing the BB simulator to throw an exception. If I run a VERY simple hello program with the following control, all is well:

RichTextField rtfHello = new RichTextField("Hello There !!!");        
add(rtfItemDescription);

But if I add a concatenated string, the entire app breaks:

String MyName = "John Doe";
RichTextField rtfHello = new RichTextField("Hello There !!!" + MyName);        
add(rtfItemDescription);

So what am I doing wrong? Why would the simulator throw an exception for the second example?

最佳回答

Not sure why it would blow up (but I m not a blackberry/java developer). Have you simply tried:

String MyName = "John Doe"; 
String HelloString = "Hello There !!!";
RichTextField rtfHello = new RichTextField(HelloString.concat(MyName));         
add(rtfItemDescription); 

Or simply,

String MyName = "John Doe"; 
RichTextField rtfHello = new RichTextField("Hello There!!!".concat(MyName));         
add(rtfItemDescription); 
问题回答

For string concatenations in Blackberry try to use StringBuffer class. StringBuffer is faster than String, because it mutable.

looks as that when i do any string concatenation in my blackberry project (using eclipse), the packaging process cannot find the definition of the class StringBuilder. the project is defaulted to jdk 1.4. StringBuilder wasn t introduced until version 1.5 so attempting to run the code will produce a "no class definition found" error message. So this is my guess as to why i am seeing my errors. – sexitrainer Jan 20 at 15:30

This is because StringBuilder is not a part of J2ME. You won t be able to use it while programming for mobile devices.

Try this

String MyName = "John Doe";
    RichTextField rtfHello = new RichTextField("Hello There !!!" + MyName);        
    add(rtfHello);

I don t think problem is with string concatenation. can provide more information like what exception you are getting.





相关问题
How does AlertListener work on Blackberry API 5.0?

Does the AlertListener interface works globally on all alerts on the blackberry device, or does it only work with alerts generated by your application? In other words, can I use this interface to ...

How to buffer audio in Blackberry?

I need to learn how to buffer audio stream from a remote server in Blackberry. There is a sample buffered playback app with Blackberry api but can you tell what url may I use to test the application?

Blackberry push notification implementation

How does one implement a push notification for a blackberry app? I heard that in order to do so I need to purchase a Blackberry Enterprise Server which costs me 1400 per year. Is this true? Where is ...

热门标签