Error When creating an Application class in Android - android

I followed the advice given on the internet regarding working with Application classes in android.
I also added a field "android:name=.Session" to my Application object in the manifest.
The application is called "Session".
What do you think is the error? This is happening even before the my App starts.
This is the error stack:
**java.lang.RuntimeException: Unable to instantiate application com.appfire.Session: java.lang.InstantiationException: com.appfire.Session**
at android.app.ActivityThread$PackageInfo.makeApplication(ActivityThread.java:523)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3996)
at android.app.ActivityThread.access$2900(ActivityThread.java:119)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1901)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4363)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
at dalvik.system.NativeStart.main(Native Method)
**Caused by: java.lang.InstantiationException: com.appfire.Session**
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1479)
at android.app.Instrumentation.newApplication(Instrumentation.java:957)
at android.app.Instrumentation.newApplication(Instrumentation.java:942)
at android.app.ActivityThread$PackageInfo.makeApplication(ActivityThread.java:518)

Make sure your Session class is public, is not static, and does not have a default constructor.
public class Session extends Application {}

In the Manifest application tag, make sure you include the fully-qualified class name of your custom Application object:
<application
android:name="com.mycompany.myapp.Session"
.... />
The Android docs do say the class name must be fully qualified:
http://developer.android.com/guide/topics/manifest/application-element.html#nm
(This is not the case for Activity class names that are included in the activity tag of the Manifest. Those can be relative, preceded with a '.').

My solution was to delete the whole project from Eclipse and load it again from SVN. Now it works

Related

java.lang.NoClassDefFoundError on an Activity

I receive a crash report from my production app, which tells me that there is an NoClassDefFound Exception on one of my activity.
The exception is thrown on this line:
final Intent i = new Intent(MainActivity.this, SomeActivity.class);
Where it claims that "SomeActivity" class is not found.
There is only one instance reported out of thousands of users. (On a Samsung Note Mini)
Which means it shouldn't be a compilation or wrong lib included issue.
Is there any clues for me to continue my investigation?
p/s: The activity is definitely defined in manifest. Else it won't even works on other user's phones.
Stack:
java.lang.NoClassDefFoundError: com.sensored.SomeActivity
at com.sensored.MainActivity$2.onItemClicked(MainActivity.java:920)
at com.sensored.adapter.SomeListAdapter$2.onClick(SomeListAdapter.java:115)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3697)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:853)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
at dalvik.system.NativeStart.main(Native Method)
1) Keep all activities under same package[say com.compname.proj.views].
2) Ensure in manifest, package attribute is set to the above package[i.e com.compname.proj.views].
3) In manifest, under all activity tag, make sure android:name attribute has values matching to below pattern:
android:name=".<your activity class name>"
Ex: android:name=".SomeActivity"
My point is not to use package name + activity class name for manifest activity declaration.

Crash ConcurrentModification

I have ocassional crash at my emulator, but for now never on tablet on which I am testing.
java.util.ConcurrentModificationException
at java.util.LinkedHashMap$LinkedHashIterator.nextEntry(LinkedHashMap.java:346)
at java.util.LinkedHashMap$EntryIterator.next(LinkedHashMap.java:375)
at java.util.LinkedHashMap$EntryIterator.next(LinkedHashMap.java:373)
at java.util.HashMap.writeObject(HashMap.java:1024)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1229)
at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1215)
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1587)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1859)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1701)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1665)
at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1153)
at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:420)
at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1251)
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1587)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1859)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1701)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1665)
at android.os.Parcel.writeSerializable(Parcel.java:1155)
at android.os.Parcel.writeValue(Parcel.java:1114)
at android.os.Parcel.writeMapInternal(Parcel.java:479)
at android.os.Bundle.writeToParcel(Bundle.java:1552)
at android.os.Parcel.writeBundle(Parcel.java:493)
at android.app.ActivityManagerProxy.activityPaused(ActivityManagerNative.java:1574)
at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3292)
at android.app.ActivityThread.access$2500(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2040)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
It seems from the log it happens when the activity is going to pause.
All my activity are accessing a common super class static object (global data), which indeed has some linkedhashmap inside, and I am saving this globaldata with putSerializable inside onSaveInstanceState. I have tried to put save/restore of globaldata inside my base activity class inside sattic synchronized methods, but it didn't helped.
My I know from yours experiences if this is some internal android bug (maybe just emulator bug) or If I am managing saving of global data (which has LinkedHashMap inside) wrong. Or is there another way to share common data between activites?
Thanks

Runtime Exception Unable to instantiate application class not found in loader?

Following is Error Log I am getting,
java.lang.RuntimeException: Unable to instantiate application
com.test.MyApplication: java.lang.ClassNotFoundException:
com.test.MyApplication in loader
dalvik.system.PathClassLoader[/mnt/asec/com.test-1/pkg.apk]  at
android.app.ActivityThread$PackageInfo.makeApplication(ActivityThread.java:659)
at
android.app.ActivityThread.handleBindApplication(ActivityThread.java:4201)
at android.app.ActivityThread.access$3000(ActivityThread.java:125)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2071)
at android.os.Handler.dispatchMessage(Handler.java:99)  at
android.os.Looper.loop(Looper.java:123)  at
android.app.ActivityThread.main(ActivityThread.java:4627)  at
java.lang.reflect.Method.invokeNative(Native Method)  at
java.lang.reflect.Method.invoke(Method.java:521)  at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.j…
That's just saying it can't find that class. Make sure the name and package are correct.
Then go to Project > Clean.. and clean/rebuild your application.
It might also make it easier for others to help if you paste the code for that Activity.
I would imagine that the class "MyApplication" extends Application. Sooo...what you most likely forgot to do was this:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:name="MyApplication" >
You need to define your custom Application class in the AndroidManifest. I would bet this will fix your problem.

How to resolve Exception during suite construction?

I have followed the below steps to test my app.
I have created Android Project.
I have created the debug signature for my sample app and test app(which need to be tested)
Place the sampleapp_debug.apk in the bin folder (where my workspace is there)
Installed the testapp_debug.apk in the emulator.
Now i am running the project as a Android JunitTest.
I am getting the below error.. How to resolve this..
java.lang.RuntimeException: Exception during suite construction
at android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests.testSuiteConstructionFailed(TestSuiteBuilder.java:239)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:520)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)
Caused by: java.lang.NullPointerException: Method name must not be null.
at java.lang.ClassCache.findMethodByName(ClassCache.java:297)
at java.lang.Class.getMethod(Class.java:985)
at android.test.suitebuilder.TestMethod.getAnnotation(TestMethod.java:60)
at android.test.suitebuilder.annotation.HasMethodAnnotation.apply(HasMethodAnnotation.java:39)
at android.test.suitebuilder.annotation.HasMethodAnnotation.apply(HasMethodAnnotation.java:30)
at com.android.internal.util.Predicates$OrPredicate.apply(Predicates.java:106)
at android.test.suitebuilder.annotation.HasAnnotation.apply(HasAnnotation.java:42)
at android.test.suitebuilder.annotation.HasAnnotation.apply(HasAnnotation.java:31)
at com.android.internal.util.Predicates$NotPredicate.apply(Predicates.java:122)
at android.test.suitebuilder.TestSuiteBuilder.satisfiesAllPredicates(TestSuiteBuilder.java:254)
at android.test.suitebuilder.TestSuiteBuilder.build(TestSuiteBuilder.java:190)
at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:373)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4218)
at android.app.ActivityThread.access$3000(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2071)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
I faced similar type of problem before. I resolved the same by using default constructor in Testcode rather than parametrized constructor. The following tutorial was useful for me
See this tutorial
I encounter the same error and this solution worked for me:
Do not use public TestTextView(String name) but public TestTextView()
as constructor.
as what W.Elsinga mention on his comment found here Link
Your referenced code was way old.
Just add default constructor.
public StartActivityTest() {
super(StartActivity.class);
}
I think the problem at method: android.test.suitebuilder.TestMethod.getAnnotation().
Try to call the method setName() at constructor of the activity.
See: RuntimeException while using ActivityUnitTestCase, but not while ActivityInstrumentationTestCase2
The exception is caused by android.test.suitebuilder.TestMethod.getAnnotation(), too.

Android intermittent class not found run time exception

One or two of my users experience the above class not found run time exception when trying to run my app.
How can that happen and how can I fix it? Is it a bad install?
Thanks
Paul
Stack trace received through Android Publishers site. I cannot replicate this myself.
(com.android.app.app.app) is the name of the app/activity as defined in the manifest.
java.lang.RuntimeException: Unable to
instantiate activity
ComponentInfo{my,app.app.app/com.android.app.app.app}:
java.lang.ClassNotFoundException:
com.android.app.app.app in loader
dalvik.system.PathClassLoader[/mnt/asec/my,app.app.app-1/pkg.apk]
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2753)
at
android.app.ActivityThread.access$2500(ActivityThread.java:129)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2107)
at
android.os.Handler.dispatchMessage(Handler.java:99)
at
android.os.Looper.loop(Looper.java:143)
at
android.app.ActivityThread.main(ActivityThread.java:4701)
at
java.lang.reflect.Method.invokeNative(Native
Method) at
java.lang.reflect.Method.invoke(Method.java:521)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at
dalvik.system.NativeStart.main(Native
Method) Caused by:
java.lang.ClassNotFoundException:
com.android.app.app.app in loader
dalvik.system.PathClassLoader[/mnt/asec/my,app.app.app-1/pkg.apk]
at
dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
at
java.lang.ClassLoader.loadClass(ClassLoader.java:573)
at
java.lang.ClassLoader.loadClass(ClassLoader.java:532)
at
android.app.Instrumentation.newActivity(Instrumentation.java:1021)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2651)
... 11 more
I'd check to see if these users are installing the app on an SD card: if the app is installed the app icon might be visible in the application list (since that is stored on-device) but if the SD card is unavailable (removed or maybe mounted for reading by a PC over USB) when the user taps the application icon the attempts to load will fail. You will notice:
ComponentInfo{my,app.app.app/com.android.app.app.app}: java.lang.ClassNotFoundException: com.android.app.app.app in loader dalvik.system.PathClassLoader[/mnt/asec/my,app.app.app-1/pkg.apk] at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659) at
in your stack trace above: is /mnt/asec the SD card storage path on the device?

Categories

Resources