Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 19,628
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to store Volume Int?

Where is the constructor for StoreInfo?

Trusted Contributor
Posts: 115
Registered: ‎03-24-2011
My Device: 8310 curve
My Carrier: PLAY

Re: How to store Volume Int?

[ Edited ]

Ok, more code :smileyhappy:

 

public MyClass() {
 
      choiceField = new ObjectChoiceField("Text", new String[]{"SomeText"});
      choiceField.setChangeListener(this);
      add(choiceField);
        
      
       buttonplay = new ButtonField("PLAY", FIELD_HCENTER | ButtonField.CONSUME_CLICK);
       buttonplay.setChangeListener(this);
       add(byttonplay);
     
     
     
     _volumeField = new EditField("Volume: ",""+_currentVolume+"%",20,EditField.READONLY);
 
    
      
      Get();         
     
       
} END PUBLIC CLASS

 

public void fieldChanged(Field field, int context) {
    
  
     if (field == buttonplay)
    
   {
      
     switch (choiceField.getSelectedIndex())
       
         {
            
     case 0: 
       
 
  try{
      {
        if (_player.getState() == Player.STARTED)
                {
                   _player.removePlayerListener(this);
     _player.stop();
     _player.deallocate();
     _player.close();
     _player = null;
                  
}
                        }
                    }
        catch (Exception ex)
       {
         System.out.println("Exception: " + ex.toString());
                        }
                       
                        try
                        {
                           
                        
                           
                      
         _player = Manager.createPlayer("URL");  
        _player.addPlayerListener(this);
         _player.realize();
         setVolume(_currentVolume);
         _player.prefetch();
          _player.start();
                       
     }

    catch (Exception ex)
                        {
                            System.out.println("Exception: " + ex.toString());
                        }
break;

}
}
} // END FIELD CHANGED

 

  
     Player _player;
     VolumeControl volumeControl;
 
    
   
   
String str = "30";
 int _currentVolume = Integer.parseInt(str);

public void Save() {

 StoreInfo info = new StoreInfo();

 info.setElement(StoreInfo.SMS, basicEditField2.getText());
//info.setElement(StoreInfo.VOL, Integer.toString(_currentVolume));
info.setElement(StoreInfo.VOL, str);

  _data.addElement(info);
  synchronized (store) {
   store.setContents(_data);
   store.commit();
 }
 

   
 
 };
 
 public void Get() {
synchronized (store) {
  _data = (Vector) store.getContents();
 
  if (!_data.isEmpty()) {
StoreInfo info = (StoreInfo) _data.lastElement();



basicEditField2.setText(info.getElement(StoreInfo.SMS));
String.valueOf(info.getElement(StoreInfo.VOL));


 }
}
};
 
static final class StoreInfo implements Persistable {
 
  private Vector _elements;
 
 
   public static final int SMS = 0;
public static final int VOL = 1;
 
 

public StoreInfo() {
 
  _elements = new Vector(2);
 
for (int i = 0; i < _elements.capacity(); ++i) {
    _elements.addElement(new String(""));
  }  
}
 
  public String getElement(int id) {
   return (String) _elements.elementAt(id);
 }
  public void setElement(int id, String value) {
   _elements.setElementAt(value, id);
 }
 
}
 
 
 static {
store = PersistentStore.getPersistentObject (0xdec6a67096f839cL);
   synchronized (store) {
  if (store.getContents() == null) {
     store.setContents(new Vector());
     store.commit();
   }
  }
   _data = new Vector();
   _data = (Vector) store.getContents();
}

 Maybe differently ... how to save String str = "30";  in persistent store?

 

I try this code for Save in persistent Store String:  (String str = "30":smileywink:

 

info.setElement(StoreInfo.VOL, str);

 Right?

 

 

When use this code:

 

public void Save() { 

 StoreInfo info = new StoreInfo(); 

 info.setElement(StoreInfo.SMS, basicEditField2.getText());

info.setElement(StoreInfo.VOL, str);

  _data.addElement(info); 
  synchronized (store) { 
   store.setContents(_data); 
   store.commit(); 
 }
 
 //Dialog.inform("Success!"); 
   
  
 }; 
 
 public void Get() { 
synchronized (store) { 
  _data = (Vector) store.getContents(); 
 
  if (!_data.isEmpty()) { 
StoreInfo info = (StoreInfo) _data.lastElement();



basicEditField2.setText(info.getElement(StoreInfo.SMS));
Integer.parseInt((String)info.getElement(StoreInfo.VOL));


 } 
} 
};

 I got this error:

 

guid:0x9C3CD62E3320B498 time: Mon May 28 14:54:38 2012  severity:1 type:3 app:Java Exception data:
	NumberFormatException
	null
	net_rim_cldc-18(4BA7D958)
	 NumberUtilities
	 parseInt
	 0x78F9
	net_rim_cldc(4BA7D958)
	 Integer
	 parseInt
	 0x7306
	net_rim_cldc(4BA7D958)
	 Integer
	 parseInt
	 0x7318

 

 

 


 
 
 
 
 
Developer
Posts: 19,628
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to store Volume Int?

My question still stands - you expect there to be tw elements in StoreInfo, but never enforce it.

 

In addition:

a) this looks dodgy:

 if (store.getContents() == null) {
     store.setContents(new Vector());
     store.commit();
   }

b) Also this could well cause you problems:

_elements = new Vector(2);
for (int i = 0; i < _elements.capacity(); ++i) {
    _elements.addElement(new String(""));
  }  
}

I think a string of "" will cause parseInt to fail in the way you have described.

 

Finally, when you are debugging, the environment should stop with a 'break point' on the failing line.  Investigating the variabsle adn situation at that point willhelp you figure out this problem.  If you don't know how to use breakpoints to work through your code, then perhaps this will help:

http://supportforums.blackberry.com/t5/Java-Development/How-to-Debug-and-Optimize/ta-p/442791