Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run the main method of an Android Activity class?

Tags:

java

android

I'd would like to examine a private method of an Android Activity by executing it within the Activity's public static void main(String[] args) method which I created.

I use Eclipse ADT and my naive approach of running the Activity as a Java application resulted in:

A fatal error has been detected by the Java Runtime Environment:
Internal Error (classFileParser.cpp:3174), pid=2936, tid=2980
Error: ShouldNotReachHere()

So I looked at the Run Configuration and found out that Android 3.1 is the sole entry in the Bootstrap Entries section of Classpath. I managed to configure the Build Path of the project so that the JRE is in the Bootstrap Entries too. Then I removed the Android 3.1 entry and added android.jar to User Entries.

The result of executing the Run Configuration is a RuntimeException:

Exception in thread "main" java.lang.RuntimeException: Stub!
at android.content.Context.(Context.java:4)

An alternative of executing some tests would be to fire up a JUnit test. But in the case of a private method this is cumbersome.

Is there a way to successfully run the main method of an Android Activity class?

like image 363
rmoestl Avatar asked Jun 03 '12 07:06

rmoestl


People also ask

Is there a main method in Android?

The main() method is in the Android framework class android. app. ActivityThread . This method creates the Main (UI) Thread , sets up a Looper on it and starts the event loop.

Where is main method in Android project?

In the case of Android, the JVM locates the main method in the ActivityThread . It then invokes the method, at which point the kernel hands over control to your application.

How can I run a particular activity in Android Studio?

Go to "Edit Configurations..." in the "Run" menu. In the left pane, select your application. In the right pane, in the "General" tab, in the "Launch Options" section, there is a "Launch:" dropdown. Select "Specified Activity", and enter the name of your activity as it appears in your Manifest.

Why there is no main method in Android?

There's one, but you don't need to see it. Android takes care of threading and keeping threads organized for you, see here. Moreover, the entry point for an activity is the onCreate() method, so all initialization is normally done in there.


1 Answers

There is another option for the problem at hand, if the private method - which should be examined through the execution of a main method - can be extracted to another class. This of course means that the method suddenly became at least protected.

But if the method is definded within a class that does not derive from android.app.Activity a main method can be defined. You only have to adjust the Run Configuration in Eclipse.

  1. Run the class with Run as Java Application. This generates a new Run Configuration named like the class
  2. Edit the newly creaded Run Configuration
  3. Delete the Android library from Bootstrap Entries in the Classpath tab
  4. Add JRE 6 or so to the build path of the project
  5. Add this JRE to the Bootstrap Entries in the Classpath tab
  6. Add android.jar, which resides in the platforms directory of the Android SDK in User Entries in the Classpath tab
like image 51
rmoestl Avatar answered Sep 19 '22 05:09

rmoestl