Android: this.getApplication() returns NULL pointer - android

I'm running the following line in an Activity, which is within the same application, but in a different package:
AppObject appObj = (AppObject)this.getApplication();
// FYI: AppObject is my extension class of Application.
It returns only a null pointer, while when I move it to the "main" package and run it from there it returns the application reference as expected.
I've defined the activity in my AndroidManifest.xml with the full qualified class name, since it is in another package: <activity android:name="com.foo.bar.TestActivity"></activity>
Update: As suggested in a question below android:name="AppObject" was already in the <application> tag of the AndroidManifest.xml

It's important to call getApplication() in the activity's onCreate() method, not in the constructor.

You need the update application tag to AndroidManifest.xml with your class name, which is extended from Application, with proper package name.
<application android:name=".AppObject">
As per Application tag google docs, Base class for those who need to maintain global application state. You can provide your own implementation by specifying its name in your AndroidManifest.xml's tag, which will cause that class to be instantiated for you when the process for your application/package is created.
here

Just run into the same thing, having refactored all my code still got the same issue, noticed that I was setting the local mApplication variable in the constructor, it should go in the onCreate(), I think all the objects in the manifest may be constructed first before getApplication() is setup so you need to call getApplication() in or after onCreate() has been called. Haven't refactored all my code back again to see if this works for different packages (sigh).

I think, it isn't a null pointer, but your function which you want to use next in the class AppObject maybe wrong.

Related

Android Activity::onCreate called before Application.onCreate

In some cases I can see that Activity.onCreate is called before the Appication object gets created (before Application.onCreate is called). Is that ever possible?
May be you forgot to add your application class in manifest file.
Place your application class in AndroidManifest.xml class under <application> tag.
i.e.,
<application
android:name=".{YourApplicationClassName}"
...
...
In some cases I can see that Activity.onCreate is called before the
Appication object gets created (before Application.onCreate is
called).
This is not what Android document says about the Application class. As per the official android documents,
The Application class, or your subclass of the Application class, is
instantiated before any other class when the process for your
application/package is created.
Also below is specific explanation of onCreate() of an Application class
Called when the application is starting, before any activity, service,
or receiver objects (excluding content providers) have been created.
Hence the onCreate() of Application has to be invoked 1st and then onCreate() of Activity class
So the scenario you have mentioned is not possible as per the flow of instantiation of Application class and Activity class
In the case you are using logging to determine when application is created, check if you are using external logging system, like Timber which is usually instantiated in the end of application onCreate(). So it may appear that nothing before this system is instantiated when it is called.
Try to instantiate logging tool before super.onCreate() in application onCreate().

Class extending Application in Android project library?

I have a project (in Eclipse) which I've turned into an Android Project Library so as to re-use some of the code in another similar project. I think I've shot myself in the foot however as I'm getting the error:
Unable to start activity ComponentInfo{com.test.scroller1/com.lib.scrolltest.ScrollTestActivity}: java.lang.ClassCastException: android.app.Application cannot be cast to com.lib.scrolltest.resAppVars
com.lib.scrolltest is my Project Library which instantiates a class extending Application (resAppVars). In the onCreate() method I call:
mRav = (resAppVars) getApplicationContext ();
This way, I can use methods in the mRav object which would otherwise be a lot of duplicated code in other classes (such as passing a query to a generic select statement which returns an ArrayList of results).
What's the problem here? It seems I've hit a limitation in the way I've implemented the Application class.
Calling getApplicationContext() returns the Application object for the current application (i.e. the application that owns the activity that onCreate() is running inside of).
Unless you're doing something strange, you don't get to pick which Application class is used. There's even a note in the documentation for Application saying not to do this:
There is normally no need to subclass Application. In most situation,
static singletons can provide the same functionality in a more modular
way. If your singleton needs a global context (for example to register
broadcast receivers), the function to retrieve it can be given a
Context which internally uses Context.getApplicationContext() when
first constructing the singleton.
You should just create a regular shared class inside of your library project. Or if you don't have a need for the special functionality library projects offer, you can also just use a regular .jar file.
And if you need shared state, just make it a singleton. ;)
Although this is a very old post. I encountered the same problem and solved it. So I thought I'll post the solution for everyone.
It turns out that I forgot to declare the subclassed application name in the manifest file.
The android:name should point to the extended app class, even if it is defined in the referenced library.
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:name="com.example.lib.MyApp">
After I added that I could use the extended app with (<cast>) getApplication() anywhere in my project.

Open an activity without declaring it in the manifest file in ANDROID?

I want to open an activity without declaring it in an manifest file.
I don't know if it is possible or not.
What I actually want is to dynamically open an activity from my program using intents.
Can anyone help me if it is possible.
Not possible. Although I am unsure what you mean "dynamically open an activity".
See: http://developer.android.com/reference/android/app/Activity.html
Under Class Overview it states "To be of use with Context.startActivity(), all activity classes must have a corresponding declaration in their package's AndroidManifest.xml"
You can have an Activity in your package and not define it in the manifest, however you would not be able to start it successfully.
Your 'Dynamic' activity start is actually the normal way of starting an activity (as you have said in a comment to the answer of Matt M). Though you HAVE to add the Activities in manifest as Matt M said. In list view, clicking an activity will call a function that will start respective activity with startActivity() function.
I tried this very long, but since the Instrumentation class uses the IActivityTaskManager, which is a class of the internal API located at com.android.server.wm, that is not in the Activity classloader, I solved this by another method:
Using a subclass
I just made an Gist, with sample code.
GIST

Android Application sub-class constructor not being called

I have a class in my Android application that sub-classes the AndroidApplication object. The documents say:
public void onCreate ()
Since: API Level 1 Called when the
application is starting, before any
other application objects have been
created. Implementations should be as
quick as possible (for example using
lazy initialization of state) since
the time spent in this function
directly impacts the performance of
starting the first activity, service,
or receiver in a process. If you
override this method, be sure to
call super.onCreate().
I placed a breakpoint on my sub-class's constructor and when I run my application, it is never reached. Naturally, when I call the sub-class's getInstance() method from other code it returns NULL since the instance variable is (supposed to be) initialized when the constructor is called.
Can anyone tell me what is wrong? I would assume from the docs that I don't have to create an instance of the AndroidApplication sub-class myself, or do I? Am I supposed to modify my manifest file somehow to add the AndroidApplication sub-class and if so, how?
-- roschler
I'm posting the answer here for others. Yes you need to add the name of your Application object sub-class's name to the Android manifest. For Eclipse users, the easiest way to do this is to open the AndroidManifest.xml file, select the Application tab in the manifest editor, and use the Browse button next to the Name field to find your Android Application object sub-class name and select it. The manifest file will be updated properly to register it. I just did that and it worked.
I had a problem of not having a . before my application class name.
Should be:
android:name=".MyApp"
since the MyApp class is in the package defined in the manifest.

ClassNotFoundException after changing app's name in Android

I have an app with main class called com.quarlityaq.quarlityaqcall.QuarlityaqCall.
After finishing programming I've changed the app name to "JCall for jobs"
by changing app_name under strings.xml to "JCall for jobs"
android:label="#string/app_name"
android:name="#string/app_name"
On my device it works just fine, but on some other devices it crashes and throws:
java.lang.RuntimeException: Unable to instantiate application com.quarlityaqcall.QuarlityaqCall.JCall for jobs:
java.lang.ClassNotFoundException: com.quarlityaqcall.QuarlityaqCall.JCall for jobs
in loader dalvik.system.PathClassLoader[/data/app-private/com.quarlityaqcall.QuarlityaqCall-1.apk]
Caused by: java.lang.ClassNotFoundException:
com.quarlityaqcall.QuarlityaqCall.JCall for jobs in loader
dalvik.system.PathClassLoader[/data/app-private/com.quarlityaqcall.QuarlityaqCall-1.apk]
I don't understand why it looks for the class name "JCall for jobs" instead of "QuarlityaqCall", and why it only happens on some devices?
Application name in manifest refers to your Application class. If you have not implemented your own Application class you should not have name attribute in in your manifest file. If you have implemented one ou should make sure that the attribute refers to correct class.
You should not export the name attribute to strings.xml as it is a technical parameter for your app. Label is the one that is visible to users.
This error can also be caused by a call to getApplicationContext() (or similar exercise that requires this context, such as connecting an object via getSharedPreferences) within the application class' declaration method.
Move these calls to an override of the 'onCreate' method (or something similar). You get a null pointer to the application context if you call for it before the application context has been fully instantiated - by moving this code to a method/function 'deeper' in the 'instantiation stack' you will be accessing a non-null context (one that has been fully instantiated).

Categories

Resources