简体   繁体   中英

Error with manifest in android (ERROR: Unable to instantiate activity ComponentInfo )

I am writing an Android application and all of a sudden it gets error from the manifest and the application did not come up anymore! I have searched a lot and I have tried many many things but I have no idea what the problem is. I put the manifest class here. Can anyone help me with this? Thanks in advance

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

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

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

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

EDIT: added MainActivity source

MainActivity.java

package Activity;

import com.example.ruby.R;
import android.app.Activity;
//rest of imports

    public class MainActivity extends Activity {

         private Button ProductBtn;
         //rest of variables

         @Override
         protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.activity_main);

             //rest of the code
         }
    //rest of the code
    }

Here is the logcat:

错误logcat

Remove Activity word that appears in front of MainActivity, ToolsActivity and AboutActivity - so instead of:

android:name="Activity.MainActivity"

use

android:name=".MainActivity"

The same with ToolsActivity and AboutActivity.

From the docs for android:name:

The name of the class that implements the activity, a subclass of Activity. The attribute value should be a fully qualified class name (such as, "com.example.project.ExtracurricularActivity"). However, as a shorthand, if the first character of the name is a period (for example, ".ExtracurricularActivity"), it is appended to the package name specified in the element.

EDIT:

Ok, the problem is in your package name. All your source files reside in package called Activity, hence you had Activity.MainActivity etc. BUT...in your manifest, you specified package as com.example.ruby.

By convention, package names should be in the format com.companyname.applicationname, in your case com.example.ruby. So what you have to do is:

  1. Keep your manifest as it is, no need to change anything (except, of course, for removing the word Activity in front of your activities, as said in my original post).
  2. Change the package name where your source files (MainActivity.java, ToolsActivity.java etc.) are located from Activity to com.example.ruby (notice how it's the same as in manifest).
  3. In each of those source files, at the top, you have line:

     package Activity; 

Change it to:

    package com.example.ruby;

Read more about packages here .

If that's your entire manifest file, you're missing a

</manifest>

end tag at the bottom.

For activity tag for name attribute you should give complete file path, or relative to package file path if its same package

<activity
    android:name=".MainActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

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

I believe that you have appended Activity onto your android:name attribute on the activity tag:

<activity
    android:name="Activity.MainActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

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

Change android:name to "MainActivity".

If the Activity's name is not in the same package as specified in the package attribute from the manifest tag, specify the package like so: "com.example.MainActivity"

Jonathan

Exception says that, ClassNotFoundException and idicates the MainActivity with the package name is not found,so made some changes,it will work now:

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

<application
    android:allowBackup="true"
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity

        android:name="com.example.ruby.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

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