简体   繁体   中英

New to Android: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo

This is my MyTasteActivity code:

package MyTaste;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MyTasteActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         TextView tv = new TextView(this);
         tv.setText("Hello, Android");
         setContentView(tv);

    }
}

This is my XML Android Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="My.Taste.App"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".MyTaasteActivity"
            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>

So why do I keep getting this error:

06-14 20:22:48.779: E/AndroidRuntime(749): FATAL EXCEPTION: main

06-14 20:22:48.779: E/AndroidRuntime(749): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{My.Taste.App/My.Taste.App.MyTasteActivity}: java.lang.ClassNotFoundException: My.Taste.App.MyTasteActivity

06-14 20:22:48.779: E/AndroidRuntime(749):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)

06-14 20:22:48.779: E/AndroidRuntime(749):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)

06-14 20:22:48.779: E/AndroidRuntime(749):  at android.app.ActivityThread.access$600(ActivityThread.java:123)

06-14 20:22:48.779: E/AndroidRuntime(749):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)

06-14 20:22:48.779: E/AndroidRuntime(749):  at 
android.os.Handler.dispatchMessage(Handler.java:99)

06-14 20:22:48.779: E/AndroidRuntime(749):  at android.os.Looper.loop(Looper.java:137)

06-14 20:22:48.779: E/AndroidRuntime(749):  at android.app.ActivityThread.main(ActivityThread.java:4424)

06-14 20:22:48.779: E/AndroidRuntime(749):  at java.lang.reflect.Method.invokeNative(Native Method)

06-14 20:22:48.779: E/AndroidRuntime(749):  at java.lang.reflect.Method.invoke(Method.java:511)

06-14 20:22:48.779: E/AndroidRuntime(749):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)

06-14 20:22:48.779: E/AndroidRuntime(749):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)

06-14 20:22:48.779: E/AndroidRuntime(749):  at dalvik.system.NativeStart.main(Native Method)

06-14 20:22:48.779: E/AndroidRuntime(749): Caused by: java.lang.ClassNotFoundException: My.Taste.App.MyTasteActivity

06-14 20:22:48.779: E/AndroidRuntime(749):  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)

06-14 20:22:48.779: E/AndroidRuntime(749):  at java.lang.ClassLoader.loadClass(ClassLoader.java:501)

06-14 20:22:48.779: E/AndroidRuntime(749):  at java.lang.ClassLoader.loadClass(ClassLoader.java:461)

06-14 20:22:48.779: E/AndroidRuntime(749):  at android.app.Instrumentation.newActivity(Instrumentation.java:1023)

06-14 20:22:48.779: E/AndroidRuntime(749):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)

06-14 20:22:48.779: E/AndroidRuntime(749):  ... 11 more

The AVD will show the message "Unfortunately, MyTaste has stopped." I have no clue what is wrong, and I have reinstalled Android SDK several times.

You specified the package as:

package="My.Taste.App"

While the package specified in the code is:

package MyTaste;

I think consistancy will fix this

Edit: The awnser below shows another consistancy issue.

The java class name is different than the Manifest definition..

public class MyTasteActivity extends Activity {

android:name=".MyTaasteActivity"

Notice the double a

The error is fairly self explanatory as well...

java.lang.ClassNotFoundException: My.Taste.App.MyTasteActivity

I had the same problem, but I had my activity declared in the Manifest file, with the correct name.

My problem was that I didn't have to imported a third party libraries in a "libs" folder, and I needed reference them in my proyect (Right-click, properties, Java Build Path, Libraries, Add Jar...).

About how do you declare an activity in the manifest file, is correct use .ActivityName always that activity be in the main package.

Another cause for give the same error, but on different line numbers is that:

class MyActivity ... { private String MyString = getString(R.strings.....);

You need to initialize everything in onCreate :)

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