IllegalStateException while adding vertical field manager in another manager - blackberry

I am trying to add a list in a VerticalFieldManager and then that manager to another VerticalFieldManager. I am using it in custom tabs.First time when application starts it runs fine but when I switch to another tab and return to same it gives IllegalStateException.
I tried it in many ways but not getting what is causing the exception in adding that VerticalFieldManager.
I am using the code :
//HEADER
_bitmap = EncodedImage.getEncodedImageResource("Friends.png");
friendsBmp = new BitmapField(Constants.sizePic(_bitmap, _bitmap.getHeight(), _bitmap.getWidth()));
//add(WebBitmapField.getUrlHFM());
SCREEN_FLAG = Constants.FRIENDS_FLAG ;
//FRIENDS' UPPER TAB
friendsTabHFM =new HorizontalFieldManager();
Bitmap ConnectedUser_normal_Bmp =Constants.sizePic(EncodedImage.
getEncodedImageResource("connected_user_normal.png"),40, Display.getWidth()/2); //Bitmap.getBitmapResource("connected_user_normal.png");
Bitmap search_normal_Bmp = Constants.sizePic(EncodedImage.
getEncodedImageResource("search_normal.png"),40, Display.getWidth()/2);//Bitmap.getBitmapResource("search_normal.png");
Bitmap ConnectedUser_tap_Bmp = Constants.sizePic(EncodedImage.
getEncodedImageResource("connected_user_tap.png"),40, Display.getWidth()/2);//Bitmap.getBitmapResource("connected_user_tap.png");
Bitmap search_tap_Bmp = Constants.sizePic(EncodedImage.
getEncodedImageResource("search_tap.png"),40, Display.getWidth()/2);//Bitmap.getBitmapResource("search_tap.png");
connectedUsersTab= new CustomButtonField(ConnectedUser_normal_Bmp.getWidth(), "", ConnectedUser_normal_Bmp, ConnectedUser_tap_Bmp, ButtonField.FIELD_HCENTER );
connectedUsersTab.setChangeListener(this);
searchTab = new CustomButtonField(search_normal_Bmp.getWidth(), "", search_normal_Bmp, search_tap_Bmp, ButtonField.FIELD_RIGHT);
searchTab.setChangeListener(this);
friendsTabHFM.add(connectedUsersTab);
friendsTabHFM.add(searchTab);
if(Constants.isGetConnectedFriends){
Constants.isGetConnectedFriends =false ;
if(friendsVFM.getFieldCount()!= 0){
friendsVFM.deleteAll();
}
//GET CONNECTED FRIENDS WEB SERVICE CALL
GetConnectedFriendsInterMediater getConnectedFriendsInterMediater = new GetConnectedFriendsInterMediater(WebServiceDetails.METHOD_GET_CONNECTED_USER, Jxa.loginUserName);
PleaseWaitPopupScreen.showScreenAndWait(getConnectedFriendsInterMediater, Constants.PLEASE_WAIT_TEXT);
}else if(Constants.isGetUserByUsername){
//Constants.isGetUserByUsername = false ;
GetUserByUsernameIntermediator getUserListIntermediator=new GetUserByUsernameIntermediator(Jxa.loginUserName ,SearchUserScreen.userName);
PleaseWaitPopupScreen.showScreenAndWait(getUserListIntermediator, Constants.PLEASE_WAIT_TEXT);
}else if(Constants.isGetAllUser){
Constants.isGetAllUser = false ;
GetAllUserListIntermediator getAllUserListIntermediator=new GetAllUserListIntermediator(WebServiceDetails.METHOD_FIND_USERS,SearchUserScreen._ageRange,SearchUserScreen._status,SearchUserScreen._religion,String.valueOf(SearchUserScreen._page) ,Jxa.loginUserName);
PleaseWaitPopupScreen.showScreenAndWait(getAllUserListIntermediator, Constants.PLEASE_WAIT_TEXT);
}
if(_mainScreenVFM.getFieldCount()!=0){
_mainScreenVFM.deleteAll();
}
_mainScreenVFM.add(friendsTabHFM);
_mainScreenVFM.add(friendsVFM);
These code is for a tab in which two sub-tabs are there.For sub tabs it is running fine but for not for main tab.
One more scenario is there,when GetConnectedFriendsInterMediater is called in that I am adding the list in friendsVFM which creating the exception.
Code for that is:
GetConnectedFriendsWebService getFriendsWebService = new GetConnectedFriendsWebService(method ,userName);
Vector friendsVecList= getFriendsWebService.getFriends();
Constants.connectedUsersVector = friendsVecList ;
synchronized (UiApplication.getEventLock()) {
if(TabControlScreen.friendsVFM.getFieldCount()!=0){
TabControlScreen.friendsVFM.deleteAll();
}
TabControlScreen.friendsVFM.add(ConnectedFriends.getInstance(KingdomConnectMain.buddyList)); //HERE LIST IS ADDED
}
I have resolved the problem ,when I was switching the tab ,I was not creating new instance for friendsVFM and using the same instance which was causing the exception at that time.Now ,same exception is thrown when I am trying to add buddyList in _listVFM . I know it is due to adding the buddyList again which is already added.Is there any solution so that I can add the list without exception. Code for that:
//CREATING SINGLETON REFERENCE OF THE BUDDYLIST SCREEN
public static ConnectedFriends getInstance(BuddyListField buddyListField){
if(connectedFriends==null){
connectedFriends = new ConnectedFriends(buddyListField);
}
return connectedFriends;
}
public ConnectedFriends(BuddyListField buddyListField) {
if(_listVFM!=null){
_listVFM.deleteAll();
}
_listVFM = new VerticalFieldManager();
_listVFM.add(buddyListField);//HERE IS EXCEPTION ,BUT WANT TO ADD THE LIST //SECOND TIME TOO
}
When I am returning from another tab to sam tab it throws exception or in other words I am not able to add the list.

Illegal state exception occurs when you're trying to add fields twice as suggested by Signare also. I guess you should try this first:
friendsVFM.getManager().delete(friendsVFM);

I solved it by using getManager() on buddyList and removing that.Than again I added it as per requirement and it worked.Code for this :
if(ConnectedFriends.getInstance(KingdomConnectMain.buddyList).getManager()!= null){
ConnectedFriends.getInstance(KingdomConnectMain.buddyList).getManager().delete(ConnectedFriends.getInstance(KingdomConnectMain.buddyList));
}
TabControlScreen.friendsVFM.add(ConnectedFriends.getInstance(KingdomConnectMain.buddyList));
This code is used while calling GetConnectedFriendsWebService in the second part of codes.

Related

SWT: Integrate clickable link into StyledText

With the help of this question I was able to figure out how I can display a link inside a StyledText widget in SwT. The color is correct and even the cursor changes shape when hovering over the link.
So far so good, but the link is not actually clickable. Although the cursor changes its shape, nothing happens if clicking on the link. Therefore I am asking how I can make clicking the link to actually open it in the browser.
I thought of using a MouseListener, tracking the click-location back to the respective text the click has been performed on and then deciding whether to open the link or not. However that seems way too complicated given that there already is some routine going on for changing the cursor accordingly. I believe that there is some easy way to do this (and assuring that the clicking-behavior is actually consistent to when the cursor changes its shape).
Does anyone have any suggestions?
Here's an MWE demonstrating what I have done so far:
public static void main(String[] args) throws MalformedURLException {
final URL testURL = new URL("https://stackoverflow.com/questions/1494337/can-html-style-links-be-added-to-swt-styledtext");
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, true));
StyledText sTextWidget = new StyledText(shell, SWT.READ_ONLY);
final String firstPart = "Some text before ";
String msg = firstPart + testURL.toString() + " some text after";
sTextWidget.setText(msg);
sTextWidget.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
StyleRange linkStyleRange = new StyleRange(firstPart.length(), testURL.toString().length(), null, null);
linkStyleRange.underline = true;
linkStyleRange.underlineStyle = SWT.UNDERLINE_LINK;
linkStyleRange.data = testURL.toString();
sTextWidget.setStyleRange(linkStyleRange);
shell.open();
while(!shell.isDisposed()) {
display.readAndDispatch();
}
}
Okay I was being a little too fast on posting this question... There's a snippet that deals with exactly this problem and it shows, that one indeed has to use an extra MouseListener in order to get things working.
The snippet can be found here and this is the relevant part setting up the listener:
styledText.addListener(SWT.MouseDown, event -> {
// It is up to the application to determine when and how a link should be activated.
// In this snippet links are activated on mouse down when the control key is held down
if ((event.stateMask & SWT.MOD1) != 0) {
int offset = styledText.getOffsetAtLocation(new Point (event.x, event.y));
if (offset != -1) {
StyleRange style1 = null;
try {
style1 = styledText.getStyleRangeAtOffset(offset);
} catch (IllegalArgumentException e) {
// no character under event.x, event.y
}
if (style1 != null && style1.underline && style1.underlineStyle == SWT.UNDERLINE_LINK) {
System.out.println("Click on a Link");
}
}
}
});

Display contact address in ios using xamarin

absolute beginner with xamarin.
Followed the following tutorial to try and simply click a button to display the contact list, select a contact, and then to display firstname, surname, and address on the screen.
https://github.com/xamarin/recipes/tree/master/Recipes/ios/shared_resources/contacts/choose_a_contact
Managed to get the firstname and surname to be displayed, but cannot get the address. Constantly getting the error
Foundation.MonoTouchException: Objective-C exception thrown. Name: CNPropertyNotFetchedException Reason: A property was not requested when contact was fetched.
On the
contanct.PostalAddresses
This is the snippet of code:-
partial void UIButton197_TouchUpInside(UIButton sender)
{
// Create a new picker
var picker = new CNContactPickerViewController();
// Select property to pick
picker.DisplayedPropertyKeys = new NSString[] { CNContactKey.GivenName, CNContactKey.FamilyName, CNContactKey.PostalAddresses };
// Respond to selection
var pickerDelegate = new ContactPickerDelegate();
picker.Delegate = pickerDelegate;
pickerDelegate.SelectionCanceled += () => {
SelectedContact1.Text = "";
};
pickerDelegate.ContactSelected += (contact) => {
SelectedContact1.Text = contact.GivenName;
SelectedContact2.Text = contact.FamilyName;
SelectedContact3.Text = contact.PostalAddresses
};
pickerDelegate.ContactPropertySelected += (property) => {
SelectedContact1.Text = property.Value.ToString();
};
// Display picker
PresentViewController(picker, true, null);
}
Am i missing something?
Seem to have resolved this if anyone else is having a similar issue.
The solution was to completely close down visual studio on the mac and re-open it.
Originally, i was stopping the project, and re-building. Possibly a bug, but non of my changes where being picked up.
A simple re-start kicked it back in

"CS0162: Warning as Error: Unreachable code detected" on Razor View

I have the following code within a script tag on my razor view:
self.regions = [];
#foreach(var region in Model.OperationRegions)
{
<text>
self.regions.push({
regionid: '#region.Region_Id',
regionname: '#region.Title',
selected: ko.observable(#(Model.RegionsList.Contains(region.Region_Id).ToString().ToLower()))
});
</text>
}
self.categories = [];
#foreach(var category in Model.Categories)
{
<text>
self.categories.push({
categoryid: '#category.Category_Id',
title: '#category.Title'
});
</text>
}
For clarity, the code outside of the foreach loops and within the text tags are Javascript and the purpose of the razor code is to populate my Javascript arrays with data from the server.
When I run this I am currently getting a server error saying "CS0162: Warning as Error: Unreachable code detected"
The error is thrown on the second "foreach" in the snippet.
Surprisingly I couldn't find another question referring to this error message on an MVC razor page so I'm posting this here.
My question is why is that line of code considered to be unreachable? I will update this question if I find anything else on my page to be relevant to the issue as I try to debug.
The error has disappeared now. I had renamed a property of my model and not recompiled before trying to load the page again. Recompiling made the error go away. I have no idea how the root cause translated to the error message shown but its fixed now in any case.
This is an extremely poor way to handle this. There's no need to build an array piece by piece like this. Just convert your list to JSON.
self.regions = #Html.Raw(Json.Encode(Model.OperationRegions.Select(region => new {
regionid = region.Region_Id,
regionname = region.Title,
selected = Model.RegionsList.Contains(region.Region_Id)
})));
The only thing this can't handle is making selected an observable. However, you can simply loop through the array and fix this:
for (var i = 0; i < self.regions.length; i++) {
self.regions[i].selected = ko.observable(self.regions[i].selected);
}
However, the better approach is to use another view model:
var OperationRegionViewModel = function (data) {
var self = {};
self.regionid = ko.observable(data.regionid);
self.regionname = ko.observable(data.regionname);
self.selected = ko.observable(data.selected);
return self;
};
Then, you can just do something like:
var regions = #Html.Raw(Json.Encode(Model.OperationRegions.Select(region => new {
regionid = region.Region_Id,
regionname = region.Title,
selected = Model.RegionsList.Contains(region.Region_Id)
})));
self.regions = $.map(regions, new OperationRegionViewModel);
Or, even better build your JSON all at once:
var json = #Html.Raw(Json.Encode(new {
regions = Model.OperationRegions.Select(r => new { ... }),
categories = Model.Categories.Select(c => new { ... }),
// etc
});
Then, inject this all into your view model:
var viewModel = (function (json) {
// other stuff
self.regions = $.map(json.regions, new OperationRegionViewModel);
self.categories = $.map(json.categories, new CategoryViewModel);
// etc
})(json);

Xamarin UItest Android - Ids for images do not display in tree

This question is for Android. StyleIds have been set for images. Per the documentation, these StyleIds should appear as labels in Android. But they do not.
I have verified that the following exists -
Xamarin.Forms.Forms.ViewInitialized += (object sender, Xamarin.Forms.ViewInitializedEventArgs e) =>
{
if (!string.IsNullOrWhiteSpace(e.View.StyleId))
{
e.NativeView.ContentDescription = e.View.StyleId;
}
};
I am new to Xamarin UItests and have to solve this as the first thing. Has anyone encountered this before?
You could switch over to using AutomationId, it was introduced to Xamarin.Forms in version 2.2 (release notes)
The AutomationId property isn't used by Xamarin.Forms so it doesn't affect any functionality to use it as a unique identifier for testing.
Below is a sample using AutomationId on an image, along with a screenshot of a Repl Tree output. (Click here to read more about AutomationId)
public App()
{
// The root page of your application
var content = new ContentPage
{
Title = "XamarinUITest_Images",
Content = new StackLayout
{
VerticalOptions = LayoutOptions.Center,
Children = {
new Label {
HorizontalTextAlignment = TextAlignment.Center,
Text = "Welcome to Xamarin Forms!"
},
new Image {
Source = "circle1",
StyleId = "myStyleID",
AutomationId = "myAutomationID"
}
}
}
};
MainPage = new NavigationPage(content);
}

How to find forground application Unique ID/Name in Blackberry

I have problem in finding the current opened application name.
I used ApplicationManager class for getting the visible applications.
The application descriptors are showing the same name for all these apps Email,Messaging,Sms,call log.
They are displaying "net_rim_bb_messaging_app" for all the above apps.
I need to find a unique identifier/name for these applications(Email,messaging,sms,calllog) when they are opened.
I tried this for the past 3 days and can't find a solution.
Can you please let me know a solution for this?
ApplicationDescriptor [] appDis = manager.getVisibleApplications();
int currentForgroundAppID = manager.getForegroundProcessId();
for(int i=0;i<appIDs.length;i++)
{
if(appDis[i].getModuleName().equals("net_rim_bb_messaging_app"))
{
//print Messaging app in foreground...
}
}
the case with in the for loop above is true for every app in this list.
Email,
Text message
Call log...
But, I need to find a unique way to find the application that was opened.
Thanks In Adv,
Satish.k
following code can display current foregroundApplication name
ApplicationDescriptor[] mAppDes;
ApplicationManager appMan = ApplicationManager.getApplicationManager();
appMan.getForegroundProcessId();
mAppDes = appMan.getVisibleApplications();
for (int i = 0; i < mAppDes.length; i++) {
boolean isFG = appMan.getProcessId(mAppDes[i]) == appMan.getForegroundProcessId();
if(isFG)
{
System.out.println("This is your Foteground application Name"+mAppDes[i].getName());
}else{
System.out.println("This is your Background application Name"+mAppDes[i].getName());
}
}

Resources