2011-05-10 166 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其他文檔。