简体   繁体   中英

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

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?

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

Activity is very important from Android's point of view, Activity's lifecycle are collections of few methods which are handled by OS through out the activities' life.

public void onCreate(Bundle savedBunldeInstance)

is called as soon as the App is launched creating the activity. This is the entry point of an application in android. You must have the emulator or physical device for running Android app, Here the file is compiled 2 times , once by java compiler then by dalvik compiler .

DVM (Dalvik Virtual Machine) is located on the Android device (EMULATOR OR PHYSICAL), this is the reason why we need emulator or physical device to run it..and onCreate is the entry point for it.

On your request I am editing this post with an additional piece of information. The lines below are abstracted from http://developer.android.com/

 Android applications don't have a single entry point.
 (there's no main() function, for example).

for further details click this :

http://developer.android.com/guide/topics/fundamentals.html

The main method isn't the entry point in Android like in Java. You wanna override the

public void onCreate(Bundle savedBunldeInstance) method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM