Recently, I've noticed that for any BlackBerry project I run, the JDE throws an error with message
Exception thrown: no application instance...
I've even checked with a sample hello world project, which ends up with the same problem.
I did run clean.bat file, erase file system etc. work arounds to clear the archives, but no luck till now.
Could someone please guide me correctly what should be the fix for this?
I'm assuming the full exception is: "IllegalStateException: no application instance". Since you didn't give us many details of your code, I'll just talk about where I've commonly run into this Exception.
This is commonly caused by trying to get an application instance before you have called the Application's constructor. For example the following code will create that error:
public class HelloWorld extends UiApplication
{
public HelloWorld(){
pushScreen(new HelloWorldScreenBlank());
}
public static void main(String[] args) {
Application app = Application.getApplication();
HelloWorld theApp = new HelloWorld();
theApp.enterEventDispatcher();
}
}
This code produces the following console code:
llegalStateException
no application instance
net_rim_cldc-8(4B84A78F)
Application
getApplication
0x2EFA
HW_5$2e0(4D1A6F55)
HelloWorld
main
0x167
But the following code doesn't produce the exception:
public class HelloWorld extends UiApplication
{
public HelloWorld(){
Application app = Application.getApplication();
pushScreen(new HelloWorldScreenBlank());
}
public static void main(String[] args) {
HelloWorld theApp = new HelloWorld();
theApp.enterEventDispatcher();
}
}
This works because the Application instance is instantiated inside the constructor for the UiApplication object. Were as in the previous code we were trying to get the instance before an Application object existed.
I've commonly run into this trying to startup GPS from main() or in a static block. But there are a few classes which implicitly call getApplicaiton, so if you aren't explicitly calling getApplication then maybe one the API calls your making is. I would try to move code out of main and into your Application's constructor if you can.
Here is a Google search which will produce a list of classes from the 6.0 API which produce this exception:
throws IllegalStateException
Blackberry
site:www.blackberry.com/developers/docs/6.0.0api
Related
According to AppCenter's instructions when adding crash reporting and analytics to a Xamarin Android app:
Inside your app’s MainActivity.cs, add the following using statements.
using Microsoft.AppCenter;
using Microsoft.AppCenter.Analytics;
using Microsoft.AppCenter.Crashes;
In the same file, add the following in the OnCreate() method.
AppCenter.Start("xxxx-xxxx-xxxx-xxxx-xxxx",
typeof(Analytics), typeof(Crashes));
However, I have a splash activity that runs before MainActivity that is prone to crashing - and the crashes won't get reported if the splash activity crashes before MainActivity gets the change to start and call AppCenter.Start.
So I also added AppCenter.Start to the start of SplashActivity. Does this mean I should remove AppCenter.Start from MainActivity in case I am starting multiple instances? Or are AppCenter instances separate to each activity and I need to add AppCenter.Start to every activity in the project (e.g. including my settings page activity)?
Add a new class and inherit it from the Application class something like below:
#if DEBUG
[Application(Debuggable=true)]
#else
[Application(Debuggable = false)]
#endif
public class MainApp : Application
{
public MainApp(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
public override void OnCreate()
{
base.OnCreate();
}
}
Override the OnCreate method, now this method will be executed every time an activities OnCreate method is executed.
So you can simply add the crash analytics code here something like:
public override void OnCreate()
{
base.OnCreate();
AppCenter.Start("xxxx-xxxx-xxxx-xxxx-xxxx",
typeof(Analytics), typeof(Crashes));
}
I have a cross platform Xamarin Forms project which works fine when ran on Android or Windows Mobile.
Now I need to run it on iOS but I get an exception when the application is launched on an iPhone emulator:
"System.TypeLoadException: Could not load type 'XamarinMO.Services.DataServices' from assembly 'XamarinMO, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'."
The exception occurs early in the code , in the iOS AppDelegate on LoadApplication(new App());:
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App()); -- <<*** EXCEPTION THROWN HERE ***
ServicePointManager
.ServerCertificateValidationCallback +=
(sender, cert, chain, sslPolicyErrors) => true;
return base.FinishedLaunching(app, options);
}
I am unable to use the debugger to step into the code and the stack trace is as helpful as a chocolate kettle.
The exception does not seem to be related to the code inside my DataServices class as the execution does not even progress that far. My DataServices class seem not to even be instantiated - when I put a break point on the first line in DataServices constructor, the execution does not even get there. This indicates to me the issue is not related to the code inside the class per se (which works fine across a wide range of Android and Windows Mobile devices)
I found the cause.
I had to swap the System.Net.Http in the PCL with Microsoft.Net.Http. Apparently standard .NET System.Net.Http works fine with Android or WinPhone but not iOS.
Now the projects runs fine on all platforms.
In my MonoDroid application, when an unhandled exception occurs the application terminates without any messages. If I can reproduce the error on my local device I can debug it through Visual Studio without any problems.
However, on remote devices I am stuck for a solution.
I have tried the following in the Application class but it does not actually write my log file, unless I am running through the debugger in Visual Studio.
public override void OnCreate()
{
base.OnCreate();
AndroidEnvironment.UnhandledExceptionRaiser += new EventHandler<RaiseThrowableEventArgs>(AndroidEnvironment_UnhandledExceptionRaiser);
}
void AndroidEnvironment_UnhandledExceptionRaiser(object sender, RaiseThrowableEventArgs e)
{
// Write Log File
}
I disagree with #SpiritMachine's answer.
Mono documentation tells us:
Note: You cannot rely on the AppDomain.UnhandledException event as managed exceptions are never unhandled in MonoDroid; they are always intercepted at the Android/managed boundary within a catch(Exception) block.
Instead, I recommend that you do the following:
AndroidEnvironment.UnhandledExceptionRaiser += (sender, args) =>
{
// Do something...
};
Try something like this:
EDIT : This code cannot handle caught errors. Please see #Jim G.'s answer....
Personally, I would localise error handling to where you need it. The reason being, you don't know what your application state will be when this handler is recruited - you may be without resources that you're depending on to do the handling...
EDIT: Using simpler code.
I have a blackberry app that until recently (I upgraded to 6.0.0.600 on my torch) could be deleted. Now I can't even if I use javaloader -u erase (module in use) and the UI context menu or physical menu has no option for delete. The code below just requires an app descriptor with an alternate entry point on startup with parameters "startVibrate". The entry points code is below.
package mypackage;
import net.rim.device.api.system.Alert;
import net.rim.device.api.system.ApplicationDescriptor;
import net.rim.device.api.system.ApplicationManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.UiApplication;
/**
* This class extends the UiApplication class, providing a
* graphical user interface.
*/
public class ScheduleAppDemo extends UiApplication
{
public static void main(String[] args)
{
if (args != null && args.length > 0 && "startVibrate".equals(args[0]))
{
scheduleVibrate();
}
else
{
ScheduleAppDemo app = new ScheduleAppDemo();
app.enterEventDispatcher();
}
}
public ScheduleAppDemo()
{
ScheduleAppDemoScreen screen = new ScheduleAppDemoScreen();
pushScreen(screen);
}
public class ScheduleAppDemoScreen extends MainScreen
{
public ScheduleAppDemoScreen()
{
setTitle("Schedule app demo");
}
}
private static void scheduleVibrate()
{
Alert.startVibrate(2550);
ApplicationDescriptor current = ApplicationDescriptor.
currentApplicationDescriptor();
current.setPowerOnBehavior(ApplicationDescriptor.DO_NOT_POWER_ON);
ApplicationManager manager = ApplicationManager.getApplicationManager();
manager.scheduleApplication(current, System.currentTimeMillis()
+ 60000, true);
}
}
Basically what this shows you is that it vibrates every minute. Unfortunately evidence says it appears the Application Manager keeps the background process running during the time and then just calls it again when it's time to run occurs again. This is the sample app from RIM. Deleting all Alternate Entry Points in the Application descriptor.xml allows the app to be deleted. Other than modifying that, reloading the cods, and then deleting it; how can I delete the app.
The behavior is 'correct', in as far as this is exactly how an application with an alternate entry point would behave in the field.
When an application has an alternate entry point, deleting it only marks it for deletion. The actual deletion occurs when the device is restarted and the .cods are reloaded.
This is why, when a user tries to delete an application that has an alternate entry point with a running process in the background, a dialog box always pops up letting the user know that the application will be removed on restart, and asking whether they would like to reboot now.
Until the device is rebooted, the background process will continue to run.
I'd like to know the proper place to put GUI based sequential start-up code in my Blackberry app.
In main(), I create MyApp and enterEventDispatcher()
I have UiApplication (MyApp)
In the MyApp CTOR:
- I create a MainScreen (MyMain)
- I call pushScreen() on MyMain
When the event dispatcher starts, is there an event I can listen for in my MainScreen that will give me the event thread where I can happily do synchronous start-up tasks?
I can use invokeLater() but I want each call to block because their order is important in this phase. invokeAndWait() throws an exception in most cases where I've attempted to use it.
I've attempted the code below but I get an exception when trying to run on the "Testing 1 2 3" line.
public class MyApp extends UiApplication {
static public void main(String[] args) {
new MyApp().enterEventDispatcher();
}
public MyApp()
{
MyView theView = new MyView();
theView.startUpdateTimer();
pushScreen(theView);
Dialog.alert("Testing 1 2 3");
}
}
If it's pretty quick UI stuff (creating a screen, pushing it onto the stack), do it from the main thread before calling enterEventDispatcher. You can actually do as much as you want, just the user experience will be worse if your app takes a long time.
The thread that calls enterEventDispatcher basically becomes the event dispatch thread, so you're safe to do any GUI stuff on that thread before calling enterEventDispatcher.
Specifically, don't call invokeAndWait from the main thread - that'll cause deadlock and probably an exception.