JAVA Exception: MainActivity cannot be cast to android.app.Activity

2,957 views
Skip to first unread message

Igor Starikov

unread,
May 23, 2016, 9:58:48 AM5/23/16
to BeeWare Users
Hi,

Tried to build an app using Python-Android-template, and caught the following on run:

java.lang.ClassCastException: python.mya.main.MainActivity cannot be cast to android.app.Activity
E/AndroidRuntime(26866): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2242)
E/AndroidRuntime(26866): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
E/AndroidRuntime(26866): at android.app.ActivityThread.access$800(ActivityThread.java:151)
E/AndroidRuntime(26866): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
E/AndroidRuntime(26866): at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime(26866): at android.os.Looper.loop(Looper.java:135)
E/AndroidRuntime(26866): at android.app.ActivityThread.main(ActivityThread.java:5349)
E/AndroidRuntime(26866): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(26866): at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime(26866): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
E/AndroidRuntime(26866): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
E/AndroidRuntime(26866): Caused by: java.lang.ClassCastException: python.mya.main.MainActivity cannot be cast to android.app.Activity
E/AndroidRuntime(26866): at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
E/AndroidRuntime(26866): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2232)
E/AndroidRuntime(26866): ... 10 more

Do I need to inherit MainActivity in main.py from something or am I missing something else?

To be more specific: I've changed target to android-19 (from android-sdk-20) in ant props file and built VOC Python support file for 3.5.1.

Russell Keith-Magee

unread,
May 23, 2016, 10:07:00 AM5/23/16
to beewar...@googlegroups.com
Hi Igor,

Yes - in order for the Android app to start, you need to have a python.<appname>.main.MainActivity class that inherits from android.app.Activity.

You can find code for an example app here:

 
That example extends android.support.v4.app.FragmentActivity, but that is itself a subclass of android.app.Activity.

To be more specific: I've changed target to android-19 (from android-sdk-20) in ant props file and built VOC Python support file for 3.5.1.

The android-19 change shouldn’t make any difference; I can’t completely vouch for the 3.5.1 branch of VOC, however. I’ve been doing most of my testing and development on 3.4, so I haven’t been regularly testing the 3.5.1 branch.

Yours,
Russ Magee %-)

Igor Starikov

unread,
May 24, 2016, 7:18:58 AM5/24/16
to BeeWare Users


понедельник, 23 мая 2016 г., 20:07:00 UTC+6 пользователь Russell Keith-Magee написал:

Yes - in order for the Android app to start, you need to have a python.<appname>.main.MainActivity class that inherits from android.app.Activity.


So I switched to 3.4.4, inherited from FragmentActivity too, and that brings me to a dex path problem:

E/AndroidRuntime(31494): FATAL EXCEPTION: main
E/AndroidRuntime(31494): Process: python.mya.main, PID: 31494
E/AndroidRuntime(31494): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{python.mya.main/python.mya.main.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "python.mya.main.MainActivity" on path: DexPathList[[zip file "/data/app/python.mya.main-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
E/AndroidRuntime(31494): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2242)
E/AndroidRuntime(31494): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
E/AndroidRuntime(31494): at android.app.ActivityThread.access$800(ActivityThread.java:151)
E/AndroidRuntime(31494): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
E/AndroidRuntime(31494): at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime(31494): at android.os.Looper.loop(Looper.java:135)
E/AndroidRuntime(31494): at android.app.ActivityThread.main(ActivityThread.java:5349)
E/AndroidRuntime(31494): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(31494): at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime(31494): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
E/AndroidRuntime(31494): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
E/AndroidRuntime(31494): Caused by: java.lang.ClassNotFoundException: Didn't find class "python.mya.main.MainActivity" on path: DexPathList[[zip file "/data/app/python.mya.main-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
E/AndroidRuntime(31494): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
E/AndroidRuntime(31494): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
E/AndroidRuntime(31494): at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
E/AndroidRuntime(31494): at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
E/AndroidRuntime(31494): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2232)
E/AndroidRuntime(31494): ... 10 more
E/AndroidRuntime(31494): Suppressed: java.lang.NoClassDefFoundError: python.mya.main.MainActivity
E/AndroidRuntime(31494): at dalvik.system.DexFile.defineClassNative(Native Method)
E/AndroidRuntime(31494): at dalvik.system.DexFile.defineClass(DexFile.java:226)
E/AndroidRuntime(31494): at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:219)
E/AndroidRuntime(31494): at dalvik.system.DexPathList.findClass(DexPathList.java:321)
E/AndroidRuntime(31494): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:54)
E/AndroidRuntime(31494): ... 14 more
E/AndroidRuntime(31494): Suppressed: java.lang.ClassNotFoundException: python.mya.main.MainActivity
E/AndroidRuntime(31494): at java.lang.Class.classForName(Native Method)
E/AndroidRuntime(31494): at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
E/AndroidRuntime(31494): at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
E/AndroidRuntime(31494): at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
E/AndroidRuntime(31494): ... 13 more
E/AndroidRuntime(31494): Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

Offtopic: toga is planned to use VOC or Rubicon?

Russell Keith-Magee

unread,
May 24, 2016, 8:11:42 PM5/24/16
to beewar...@googlegroups.com
It looks like your MainActivity class isn’t being found at all. If you look in the bin/classes directory, you should find all the compiled products - there should be a python/mya/main/MainActivity.class. Does that class exist? If it doesn’t, it means the compilation hasn’t succeeded, probably because the source code for your Python class wasn’t found.

Offtopic: toga is planned to use VOC or Rubicon?

Both, but on different platforms. It will be using VOC on Android; Rubicon ObjC on iOS. Rubicon Java was my original attempt for Android support; while the approach worked really well on Desktop Java, it proved problematic on Android due to limitations of the JNI interface on Android.

Yours,
Russ Magee %-)

Igor Starikov

unread,
May 24, 2016, 10:20:25 PM5/24/16
to BeeWare Users


It looks like your MainActivity class isn’t being found at all. If you look in the bin/classes directory, you should find all the compiled products - there should be a python/mya/main/MainActivity.class. Does that class exist? If it doesn’t, it means the compilation hasn’t succeeded, probably because the source code for your Python class wasn’t found.


 Yes, it seems that all required classes are in place:

Compilation process seems OK too:


Any suggestions?


Russell Keith-Magee

unread,
May 24, 2016, 11:37:35 PM5/24/16
to beewar...@googlegroups.com
Hi Igor,

 I’m certainly running out of obvious causes :-)

Can you decompile your MainActivity.class, and confirm that it has a “static main void” method, and that it extends Activity (or FragmentActivity)? You can use voc to do this:

python -m voc.java bin/classes/python/mya/main/MainActivity.class 

It help if I can see the dump this file produces, as well as the source code.

Yours,
Russ Magee %-)

Igor Starikov

unread,
May 25, 2016, 12:36:26 AM5/25/16
to BeeWare Users

It help if I can see the dump this file produces, as well as the source code.


Sure. Here they are:

 

Igor Starikov

unread,
May 25, 2016, 12:57:19 AM5/25/16
to BeeWare Users
ADB shell run says:

$ adb shell am start -n python.mya/python.mya.MainActivity


Starting: Intent { cmp=python.mya/.MainActivity }
Error type 3
Error: Activity class {python.mya/python.mya.MainActivity} does not exist.

Russell Keith-Magee

unread,
May 25, 2016, 1:36:51 AM5/25/16
to beewar...@googlegroups.com
Hold on - that might be the problem. The class is “python.mya.main.MainActivity”, so the command line to start the app will be:

adb shell am start -n python.mya.main/python.mya.main.MainActivity

Does that work any better?

Yours,
Russ Magee %-)

Igor Starikov

unread,
May 25, 2016, 1:48:11 AM5/25/16
to BeeWare Users

Does that work any better?

Yes, that's better in a sense that ADB now tries to run the app, yet after that the behaviour is exactly the same as if I run it manually — class not found and Android fires message box saying that the app is stopped.

Russell Keith-Magee

unread,
May 26, 2016, 5:41:45 PM5/26/16
to beewar...@googlegroups.com
Hi Igor,

On Wed, May 25, 2016 at 1:48 PM, Igor Starikov <idle...@gmail.com> wrote:

Does that work any better?

Yes, that's better in a sense that ADB now tries to run the app, yet after that the behaviour is exactly the same as if I run it manually — class not found and Android fires message box saying that the app is stopped.

That’s definitely odd. It sounds very much like there’s some relatively simple path thing that isn’t set up right, but I can’t spot any obvious cause. 

I haven’t done a complete clean-room runthrough with the VOC+Android template toolchain for a little while, so it’s possible you might have chanced upon a combination that is in a broken state. I’m about to head to PyCon; the PyCon sprints should give me an opportunity to take this for a spin with a range of people who are starting from scratch. Hopefully this will clarify what the issue is.

Yours,
Russ Magee %-)

Igor Starikov

unread,
May 26, 2016, 9:41:00 PM5/26/16
to BeeWare Users


 I’m about to head to PyCon; the PyCon sprints should give me an opportunity to take this for a spin with a range of people who are starting from scratch. Hopefully this will clarify what the issue is.


Good luck at PyCon. 
Meanwhile I'll try to investigate it myself this weekends and report here if any progress.
Thank you, Russell.

Igor Starikov

unread,
May 27, 2016, 4:55:50 AM5/27/16
to BeeWare Users
Another try on decompilation, now using fernflower.

Decompiled generic - http://pastebin.com/Uk8UaES3

Some issues spotted:
  • It appers that from x.y import z don't make it into .class at all in contrast to import x.y.z.
  • Many calls to Type.pythonType("python/intelli/main/MainActivity") when type is already in var10000.
Reply all
Reply to author
Forward
0 new messages