简体   繁体   English

如何运行Android Activity类的main方法?

[英]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. 我想通过在我创建的Activity的公共静态void main(String [] args)方法中执行它来检查Android Activity的私有方法。

I use Eclipse ADT and my naive approach of running the Activity as a Java application resulted in: 我使用Eclipse ADT,而我将Activity作为Java应用程序运行的幼稚方法导致:

A fatal error has been detected by the Java Runtime Environment: Java运行时环境检测到致命错误:
Internal Error (classFileParser.cpp:3174), pid=2936, tid=2980 内部错误(classFileParser.cpp:3174),pid = 2936,tid = 2980
Error: ShouldNotReachHere() 错误: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 . 所以我查看了运行配置 ,发现Android 3.1ClasspathBootstrap Entries部分中的唯一条目。 I managed to configure the Build Path of the project so that the JRE is in the Bootstrap Entries too. 我设法配置了项目的构建路径 ,以便JRE也位于引导条目中 Then I removed the Android 3.1 entry and added android.jar to User Entries . 然后,我删除了Android 3.1条目,并将android.jar添加到User Entries中

The result of executing the Run Configuration is a RuntimeException: 执行运行配置的结果是RuntimeException:

Exception in thread "main" java.lang.RuntimeException: Stub! 线程“主”中的异常java.lang.RuntimeException:存根!
at android.content.Context.(Context.java:4) 在android.content.Context。(Context.java:4)

An alternative of executing some tests would be to fire up a JUnit test. 执行某些测试的替代方法是启动JUnit测试。 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? 有没有一种方法可以成功运行Android Activity类的main方法?

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 . 如果可以将private方法(应通过执行主方法进行检查)可以提取到另一个类 ,则还有另一种选择可以解决该问题。 This of course means that the method suddenly became at least protected . 当然,这意味着该方法至少突然protected

But if the method is definded within a class that does not derive from android.app.Activity a main method can be defined. 但是,如果该方法在不是从android.app.Activity派生的类中定义,则可以定义main方法 You only have to adjust the Run Configuration in Eclipse. 您只需要在Eclipse中调整运行配置

  1. Run the class with Run as Java Application . 使用“作为Java应用程序运行”运行类。 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 Classpath选项卡中的Bootstrap条目中删除Android
  4. Add JRE 6 or so to the build path of the project 将JRE 6左右添加到项目的构建路径
  5. Add this JRE to the Bootstrap Entries in the Classpath tab 将此JRE添加到Classpath选项卡中的Bootstrap条目
  6. Add android.jar , which resides in the platforms directory of the Android SDK in User Entries in the Classpath tab 添加android.jar ,它位于“ 类路径”选项卡中“ 用户条目”Android SDKplatform目录中。

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. 从Android的角度来看,Activity非常重要,Activity的生命周期是OS在​​整个生命周期中处理的几种方法的集合。

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. 这是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 . 您必须具有运行Android应用程序的仿真器或物理设备,此处文件被compiled 2 times ,一次是通过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. DVM (Dalvik Virtual Machine)位于Android设备(仿真器或物理设备)上,这就是我们需要仿真器或物理设备来运行它的原因。onCreate是其入口点。

On your request I am editing this post with an additional piece of information. 根据您的要求,我正在编辑此帖子,并提供其他信息。 The lines below are abstracted from http://developer.android.com/ 以下几行摘自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. 主要方法不是Java中的Android切入点。 You wanna override the 您想覆盖

public void onCreate(Bundle savedBunldeInstance) method.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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