2011-05-10 162 views
0

我试图运行在2.3.1模拟器我的第一个Hello World应用程序,但我得到了以下错误消息第一个应用程序:“该应用程序的Hello World(过程com.helloworld)已意外停止,请重再次运行在Android模拟器

有什么能发生这种情况的原因

这里是源代码:

package com.helloworld; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 

public class HelloWorldActivity extends Activity implements View.OnClickListener { 

    Button button; 
    int touchCount; 

    @Override 
    public void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     button = new Button(this); //create the Button 
     button.setText("Touch me"); //set its initial text 
     button.setOnClickListener(this); 
     setContentView(button);  
      } 

    public void onClick(View v) { 
     touchCount++; //Increase the touchCount 
     button.setText("Touched me " + touchCount + "time(s)"); 
    } 
    } 

堆栈跟踪:

05-10 17:32:18.749: ERROR/AndroidRuntime(511): FATAL EXCEPTION: main 
05-10 17:32:18.749: ERROR/AndroidRuntime(511): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.helloworld/com.helloworld.HelloWorld.activity}: java.lang.ClassNotFoundException: com.helloworld.HelloWorld.activity in loader dalvik.system.PathClassLoader[/data/app/com.helloworld-1.apk] 
05-10 17:32:18.749: ERROR/AndroidRuntime(511):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1544) 
05-10 17:32:18.749: ERROR/AndroidRuntime(511):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638) 
05-10 17:32:18.749: ERROR/AndroidRuntime(511):  at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
05-10 17:32:18.749: ERROR/AndroidRuntime(511):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928) 
05-10 17:32:18.749: ERROR/AndroidRuntime(511):  at android.os.Handler.dispatchMessage(Handler.java:99) 
05-10 17:32:18.749: ERROR/AndroidRuntime(511):  at android.os.Looper.loop(Looper.java:123) 
05-10 17:32:18.749: ERROR/AndroidRuntime(511):  at android.app.ActivityThread.main(ActivityThread.java:3647) 
05-10 17:32:18.749: ERROR/AndroidRuntime(511):  at java.lang.reflect.Method.invokeNative(Native Method) 
05-10 17:32:18.749: ERROR/AndroidRuntime(511):  at java.lang.reflect.Method.invoke(Method.java:507) 
05-10 17:32:18.749: ERROR/AndroidRuntime(511):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
05-10 17:32:18.749: ERROR/AndroidRuntime(511):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
05-10 17:32:18.749: ERROR/AndroidRuntime(511):  at dalvik.system.NativeStart.main(Native Method) 
05-10 17:32:18.749: ERROR/AndroidRuntime(511): Caused by: java.lang.ClassNotFoundException: com.helloworld.HelloWorld.activity in loader dalvik.system.PathClassLoader[/data/app/com.helloworld-1.apk] 
05-10 17:32:18.749: ERROR/AndroidRuntime(511):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240) 
05-10 17:32:18.749: ERROR/AndroidRuntime(511):  at java.lang.ClassLoader.loadClass(ClassLoader.java:551) 
05-10 17:32:18.749: ERROR/AndroidRuntime(511):  at java.lang.ClassLoader.loadClass(ClassLoader.java:511) 
05-10 17:32:18.749: ERROR/AndroidRuntime(511):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021) 
05-10 17:32:18.749: ERROR/AndroidRuntime(511):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1536) 
05-10 17:32:18.749: ERROR/AndroidRuntime(511):  ... 11 more 

http://pastebin.com/7R9pF34w

+0

你应该没有职位的错误的截图,但堆栈跟踪。 – RoflcoptrException 2011-05-10 17:08:40

+0

请在您的问题中发布所有相关的源代码,Logcat输出等,而不是使用外部网站。我无法查看图像,因为IT策略会阻止它。 – Trevor 2011-05-10 17:17:51

+0

@ Trev16v - 我修好了。 – kachilous 2011-05-10 17:25:36

回答

1

如上评论指出,问题是这条线在清单:

<activity android:name=".HelloWorld.activity" 
        android:label="@string/app_name"> 

android:name属性告诉VM什么类来查找启动活动时,但你的类是在你的java文件public class HelloWorldActivity创建。因此,当虚拟机试图实例化一个HelloWorld.activity对象时,它无法这样做,并且崩溃了一个ClassNotFoundException。解决的办法是改变上述阅读:

<activity android:name=".HelloWorldActivity" 
        android:label="@string/app_name"> 

...所以它的类定义相匹配,因此允许虚拟机来找到它。此外,导致启动时立即崩溃的原因是因为第一个活动条目被认为是“启动”活动。

您可以找到有关清单文件here其他文档。