简体   繁体   中英

Error in a simple hello world android program

I just created a simple hello world program, all codes are fine it seems, but something makes the android to show "unfortunately app needs to close" error

the startingPoint.java code which is the launcher class

package demo4;

import com.example.demo4.R;
import android.app.Activity;
import android.os.Bundle;

public class startingPoint extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    }
    }

the main.xml which is the layout for launcher class

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >



   <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

</LinearLayout>

the androidmanifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.demo4"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="9" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
          <activity
            android:name=".startingPoint"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

and this is the logcat

    08-30 12:46:22.921: D/AndroidRuntime(326): Shutting down VM
    08-30 12:46:22.941: W/dalvikvm(326): threadid=1: thread exiting with uncaught exception (group=0x40015560)
    08-30 12:46:23.021: E/AndroidRuntime(326): FATAL EXCEPTION: main
    08-30 12:46:23.021: E/AndroidRuntime(326): java.lang.RuntimeException: Unable to instantiate activity       ComponentInfo{com.example.demo4/com.example.demo4.startingPoint}: java.lang.ClassNotFoundException: com.example.demo4.startingPoint in loader dalvik.system.PathClassLoader[/data/app/com.example.demo4-2.apk]
    08-30 12:46:23.021: E/AndroidRuntime(326):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
    08-30 12:46:23.021: E/AndroidRuntime(326):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
    08-30 12:46:23.021: E/AndroidRuntime(326):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    08-30 12:46:23.021: E/AndroidRuntime(326):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
    08-30 12:46:23.021: E/AndroidRuntime(326):  at android.os.Handler.dispatchMessage(Handler.java:99)
    08-30 12:46:23.021: E/AndroidRuntime(326):  at android.os.Looper.loop(Looper.java:123)
    08-30 12:46:23.021: E/AndroidRuntime(326):  at android.app.ActivityThread.main(ActivityThread.java:3683)
    08-30 12:46:23.021: E/AndroidRuntime(326):  at java.lang.reflect.Method.invokeNative(Native Method)
    08-30 12:46:23.021: E/AndroidRuntime(326):  at java.lang.reflect.Method.invoke(Method.java:507)
    08-30 12:46:23.021: E/AndroidRuntime(326):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    08-30 12:46:23.021: E/AndroidRuntime(326):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    08-30 12:46:23.021: E/AndroidRuntime(326):  at dalvik.system.NativeStart.main(Native Method)
    08-30 12:46:23.021: E/AndroidRuntime(326): Caused by: java.lang.ClassNotFoundException: com.example.demo4.startingPoint in loader dalvik.system.PathClassLoader[/data/app/com.example.demo4-2.apk]
    08-30 12:46:23.021: E/AndroidRuntime(326):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
    08-30 12:46:23.021: E/AndroidRuntime(326):  at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
    08-30 12:46:23.021: E/AndroidRuntime(326):  at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
    08-30 12:46:23.021: E/AndroidRuntime(326):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
    08-30 12:46:23.021: E/AndroidRuntime(326):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
    08-30 12:46:23.021: E/AndroidRuntime(326):  ... 11 more

Resloved...it was as indicated by Mtoypc. the package name was different and it was not reflected in the startingPoint.java

So I post my comment as an answer :)

It seems that android try to find the class in your package com.example.demo4.startingPoint , but in your code the package is just demo4 . Try to change the first line by package com.example.demo4.

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