我正在尝试在2.3.1模拟器上运行我的第一个hello world应用程序,但是我收到以下错误消息:“应用程序Hello World(进程com.helloworld)已意外停止。请再试一次。
这是怎么回事?
以下是源代码:
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
答案 0 :(得分:1)
实际上是我最后的评论。您不使用按钮作为setContentView的布局。创建一个布局,将按钮放在布局上,执行sentContentView(R.layout.your_layout); 然后找到按钮(或将其添加到布局)
尝试按照hello world的教程进行操作:http://developer.android.com/resources/tutorials/hello-world.html
答案 1 :(得分:1)
如上面的评论中所述,问题是清单中的这一行:
<activity android:name=".HelloWorld.activity"
android:label="@string/app_name">
android:name
属性告诉VM启动活动时要查找的类,但是您的类在.java文件中创建为public class HelloWorldActivity
。因此,当VM尝试实例化HelloWorld.activity
对象时,它无法执行此操作,并使用ClassNotFoundException
崩溃。解决方案是将上面的内容改为:
<activity android:name=".HelloWorldActivity"
android:label="@string/app_name">
...以便它与您的类定义匹配,从而允许VM找到它。此外,这在启动时立即导致崩溃的原因是因为第一个活动条目被视为“启动”活动。
您可以找到有关清单文件here的其他文档。