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
Highlighted
New Developer
Posts: 10
Registered: ‎06-24-2014
My Device: BlackBerry Bold 9900
My Carrier: Free
Accepted Solution

IllegalStateException while inserting in a Manager.

Hi !

I'm trying to insert a field in a Manager from a thread.

More details : The manager in question is actually a RichList-like class I wrote for "preBBOS7" devices. Here it is : 

 

package crapsnap;

import net.rim.device.api.command.Command;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.BackgroundFactory;

public class RichListField extends VerticalFieldManager {

	private Command cmd = null;
	private boolean _imageSpaceUsed;
	private int _numberOfLabelFields;
	private int _numberOfDescriptionFields;
	private boolean alternened = false;

	RichListField(Manager manager, boolean imageSpaceUsed,
			int numberOfLabelFields, int numberOfDescriptionFields) {
		_imageSpaceUsed = imageSpaceUsed;
		_numberOfLabelFields = numberOfLabelFields;
		_numberOfDescriptionFields = numberOfDescriptionFields;
		this.setBackground(BackgroundFactory.createBitmapBackground(EncodedImage.getEncodedImageResource("empty.png").getBitmap()));
		manager.add(this);
	}

	public int getFocusRow() {
		for (int i = 0; i < this.getFieldCount(); i++) {
			if (this.getField(i).isFocus() == true) {
				return i;
			}
		}
		return -1;
	}

	public void setFocusPolicy(int policy) {
		//pas compris l'interet. 
		//Juste là pour compatibilité.
	}

	public void setCommand(Command command) {
		cmd = command;
	}

	public void remove(int row) {
		this.delete(this.getField(row));
	}

	public  void insert(int row, Object[] rowObj) {
		this.insert(Factory(rowObj, true), row);
	}

	public Object[] get(int row) {
		return ((ListManager) this.getField(row)).arg;
	}

	public void add(Object[] rowObj) {
		this.add(Factory(rowObj, false));
	}

	protected boolean trackwheelClick(int status, int time) {
		if (cmd.equals(null)) {
			return false;
		} else {
			cmd.execute(this);
			return true;
		}
	}
	
	private Manager Factory(Object[] arg, boolean isupdated) {

			int i = 0;
			boolean textfielded = false;
			ListManager Main = new ListManager();
			Main.arg = arg;
			HorizontalFieldManager Head = new HorizontalFieldManager();
			VerticalFieldManager Label = new VerticalFieldManager();
			VerticalFieldManager Description = new VerticalFieldManager();
			if (_imageSpaceUsed == true) {
				Head.add(new BitmapField((Bitmap) arg[0]));
				i = i + 1;
			}
			for (int j = 0; j < _numberOfLabelFields; j++) { 
				if (textfielded == false){
					LabelField txt = new LabelField((String) arg[i] + "                                                                                         ",Field.FOCUSABLE);
					Label.add(txt);
					textfielded = true;
				} else {
				Label.add(new LabelField((String) arg[i]));
				}
				i = i +1;
			}
			Head.add(Label);
			Main.add(Head);
			for (int k = 0; k < _numberOfDescriptionFields; k++) {
				Description.add(new LabelField((String) arg[i]));
				i = i+1;
			}
			Main.add(Description);
			if (isupdated == false){
				if (alternened == true){
				Main.setBackground(BackgroundFactory.createSolidBackground(Color.IVORY));
				alternened = false;
			} else {
				Main.setBackground(BackgroundFactory.createSolidBackground(Color.WHITE));
				alternened = true;
			}
			}else {
				Main.setBackground(BackgroundFactory.createSolidBackground(Color.ANTIQUEWHITE));
			}
			
			return Main;
	}

	private class ListManager extends VerticalFieldManager {
		
		public Object[] arg;
		
		ListManager() {
			super(Manager.NO_HORIZONTAL_SCROLL );
		}
		
	}
}

 

 

 

So it's used in the mainscreen of my app. The problem come when I try to update the screen from a tread launched in a separate class. Here it is : 

 

 

public void  run() {
			try {
				Thread.sleep(10);
				ReceveirString = Recipent + "";
				_viewSnap.Snaplist.insert(6, new Object[] { _viewSnap._0img,
						"" + ReceveirString, "Sending..." });
				_viewSnap.offsetint++;
				_viewSnap.Vectoroffset = _viewSnap.Vectoroffset + receiver;
				if (friendselector.IsStoryed() == true) {
					Thread.sleep(10);
					if (API.SendStory(encryptedImg, 0, Recipent,
							Current.getString("username"),
							Current.getString("auth_token"), time) == true) {
						_viewSnap.Snaplist.remove(6);
						_viewSnap.Snaplist.insert(6,
								new Object[] { _viewSnap._0img, "" + ReceveirString,
										"Moment ago..." });
						return;
					}
				} else {
					Thread.sleep(10);
					if (API.SendSnap(encryptedImg, 0, Recipent,
							Current.getString("username"),
							Current.getString("auth_token"), time) == true) {
						_viewSnap.Snaplist.remove(6);
						_viewSnap.Snaplist.insert(6,
								new Object[] { _viewSnap._0img, "" + ReceveirString,
										"Moment ago..." });
						return;
					}
				}
			} catch (JSONException e) {
				e.printStackTrace();
				ReceveirString = Recipent + "";
				
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

 I take the Screen that contain my RichList in parameter, that's the _viewsnap (and snaplist is my Manager inside it). But when I do that, I get "IllegalStateException"... don"t know why. Any idea ???

Developer
Posts: 17,019
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: IllegalStateException while inserting in a Manager.

use invokeLater to access the UI from a separate thread.

see
http://supportforums.blackberry.com/t5/Java-Development/What-is-the-Event-Thread/ta-p/446865
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter