190

I was trying to run a sample code While launching the application in the android 1.5 emulator , I got these errors.... Any one have some hint..?

ERROR from LogCat:

01-13 02:28:08.392: ERROR/AndroidRuntime(2888): FATAL EXCEPTION: main
01-13 02:28:08.392: ERROR/AndroidRuntime(2888): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.s.android.test/com.s.android.test.MainActivity}: java.lang.ClassNotFoundException: com.s.android.test.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.s.android.test-2.apk]
01-13 02:28:08.392: ERROR/AndroidRuntime(2888):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1544)
01-13 02:28:08.392: ERROR/AndroidRuntime(2888):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
01-13 02:28:08.392: ERROR/AndroidRuntime(2888):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-13 02:28:08.392: ERROR/AndroidRuntime(2888):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
01-13 02:28:08.392: ERROR/AndroidRuntime(2888):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-13 02:28:08.392: ERROR/AndroidRuntime(2888):     at android.os.Looper.loop(Looper.java:123)
01-13 02:28:08.392: ERROR/AndroidRuntime(2888):     at android.app.ActivityThread.main(ActivityThread.java:3647)
01-13 02:28:08.392: ERROR/AndroidRuntime(2888):     at java.lang.reflect.Method.invokeNative(Native Method)
01-13 02:28:08.392: ERROR/AndroidRuntime(2888):     at java.lang.reflect.Method.invoke(Method.java:507)
01-13 02:28:08.392: ERROR/AndroidRuntime(2888):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-13 02:28:08.392: ERROR/AndroidRuntime(2888):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-13 02:28:08.392: ERROR/AndroidRuntime(2888):     at dalvik.system.NativeStart.main(Native Method)
01-13 02:28:08.392: ERROR/AndroidRuntime(2888): Caused by: java.lang.ClassNotFoundException: com.s.android.test.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.s.android.test-2.apk]
01-13 02:28:08.392: ERROR/AndroidRuntime(2888):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
01-13 02:28:08.392: ERROR/AndroidRuntime(2888):     at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
01-13 02:28:08.392: ERROR/AndroidRuntime(2888):     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
01-13 02:28:08.392: ERROR/AndroidRuntime(2888):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
01-13 02:28:08.392: ERROR/AndroidRuntime(2888):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1536)
01-13 02:28:08.392: ERROR/AndroidRuntime(2888):     ... 11 more
01-13 02:28:08.407: WARN/ActivityManager(112):   Force finishing activity com.s.android.test/.MainActivity

Edit This error happens to most of the beginners, the thing is that you have to add all your activities in the Manifest file.

Braiam
  • 1
  • 11
  • 47
  • 78
rahul
  • 2,758
  • 5
  • 32
  • 53
  • thanks Dan Breslau...It looks better now..how to do that? – rahul Jan 14 '11 at 05:45
  • 1
    Sorry guys...sorry for waisting your time..I just forgot to add the new class to the Manifest file.. – rahul Jan 14 '11 at 06:45
  • 1
    updating all my tools (eclipse & sdk manager) fixed it for me, as well as adding android support library to project – Hayden Thring Jun 09 '13 at 09:22
  • i have this problem, see this http://stackoverflow.com/questions/30473270/why-this-runtime-is-occurd-unable-to-instantiate-activity-componentinfo – DanialAbdi May 27 '15 at 04:53

47 Answers47

175

It is a problem of your Intent.

Please add your Activity in your AndroidManifest.xml.

When you want to make a new activity, you should register it in your AndroidManifest.xml.

Mohsen Kamrani
  • 7,177
  • 5
  • 42
  • 66
Tanmay Mandal
  • 39,873
  • 12
  • 51
  • 48
  • 5
    I have a class which does not extends activity, just contains some common fuctions; but i still get this exception; why? – Manoj Kumar Sep 10 '12 at 08:09
  • In my project it says that the class is not public when looking at the android:name in xml file. When I add public before class in the main activity the app runs without problems. Weird. – Codebeat May 21 '15 at 01:21
  • i had this issue in a native app that i was developing.. i added another method in the activity class, and i started getting this error.. for me a clean + rebuild solved it.. – ashishsony May 06 '16 at 15:34
  • 12
    Another option is that your activity is in the manifest, but it is declared as `abstract` (I made that mistake during refactoring and the error message isn't helping much). – Czechnology Dec 28 '16 at 20:59
  • How do I do this? – RedGuy11 Jan 31 '21 at 23:51
76

You may be trying to find the view before onCreate() which is incorrect.

public class MainActivity extends Activity {

  ImageView mainImage = (ImageView) findViewById(R.id.imageViewMain); //incorrect

  @Override
  protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  }
  ...
}
SMUsamaShah
  • 7,677
  • 22
  • 88
  • 131
73

There is another way to get an java.lang.RuntimeException: Unable to instantiate activity ComponentInfo exception and that is the activity that you are trying to start is abstract. I made this stupid mistake once and its very easy to overlook.

Kevin
  • 1,745
  • 13
  • 16
  • 18
    Also, the Activity must be public (this was my mistake). – djjeck Jan 21 '12 at 17:00
  • Sorry to bother you two years later, but I was wondering why you can't start an abstract activity? – BenjaminFranklin Dec 29 '13 at 01:39
  • 2
    @BenjaminFranklin An abstract class means it's not complete, it's missing parts. Prefixing a class with the keyword abstract means it cannot be instantiated directly, but only through inheritance: and other class implementing the abstract class's missing features. – Kit10 Jan 10 '14 at 18:25
  • I have already inherited the abstract class but still unable to work. Would you pls explain further?? – Mohammedsalim Shivani Aug 11 '18 at 06:19
54

In my case I forgot to add the google maps library

<application>
    ....

    <uses-library android:name="com.google.android.maps" />
</application>

Also, check that you're not missing the preceding dot before the activity path

<activity android:name=".activities.MainActivity"/>
Maragues
  • 37,861
  • 14
  • 95
  • 96
28

This might not be relevant to the actual question, but in my instance, I tried to implement Kotlin and left out apply plugin: 'kotlin-android'. The error happened because it could not recognize the MainActivity in as a .kt file.

Hope it helps someone.

Muz
  • 5,866
  • 3
  • 47
  • 65
27

Image

It also happens because of this issue. I unchecked the jars that needed be exported to the apk and this same thing happened. Please tick the jars that your app Needs to run.

meowmeowbeans
  • 687
  • 7
  • 14
  • Is important to see this issue, and try each dependency component correctly. For example, on Android Studio (or Intellij IDEA) select the correct scope for the dependencies (Provided, Compile, ...). In my case, change Provided to Compile and works fine. – dayanruben Jan 21 '15 at 13:24
15

I encountered this problem too, but in a slightly different way. Here was my scenario:

App detail:

  • Using ActionBarSherlock as a library
  • Using android-support-v4-r7-googlemaps.jar in the ActionBarSherlock library so I could use a "map fragment" in my primary project
  • Added the jar to the build path of my primary project
  • Included the <uses-library android:name="com.google.android.maps" /> in the manifests of both the library project and my primary project (this may not be necessary in the library?)
  • The manifest in the primary project had the proper activity definition and all of the appropriate properties
  • I didn't have an abstract activity or any of the other gotchas I've seen on Stack Overflow pertaining to this problem.

However, I still encountered the error described in the original post and could not get it to go away. The problem though, was slightly different in one regard:

  • It only affected a fresh install of the application to my device. Any time the app installed for the first time, I would get the error preceded by several "warnings" of: Unable to resolve superclass of FragmentActivity
  • Those warnings traced back to the ActionBarSherlock library
  • The app would force close and crash.
  • If I immediately rebuilt and redeployed the app, it worked fine.
  • The only time it crashed was on a totally fresh install. If I uninstalled the app, built and deployed again from Eclipse, it would crash. Build/deploy a second time, it would work fine.

How I fixed it:

  • In the ActionBarSherlock library project, I added the android-support-v4-r7-googlemaps.jar to the build path
  • This step alone did not fix the problem

  • Once the jar was added to the build path, I had change the order on the Java Build Path > Order and Export tab - I set the JAR to the first item in the list (it was the last after the /src and /gen items).

  • I then rebuilt and redeployed the app to my device - it worked as expected on a fresh install. Just to be certain, I uninstalled it again 2-3 times and reinstalled - still worked as expected.

This may be a total rookie mistake, but I spent quite a while digging through posts and my code to figure it out, so hopefully it can be helpful to someone else. May not fix all situations, but in this particular case, that ended up being the solution to my problem.

Kyle
  • 606
  • 5
  • 16
  • Thanks! This worked for me when I got this problem when using googles admob sdk example "adcatalog" and was getting this error. An important note is to check the checkboxes next to the libraries. In my case support & admobsdk jars. – jsmars Aug 07 '13 at 07:02
11

This error can also be the ultimate sign of a dumb mistake (like when I - I mean, cough, like when a friend of mine who showed me their code once) where they try to execute code outside of a method like trying to do this:

SQLiteDatabase db = openOrCreateDatabase("DB", MODE_PRIVATE, null); //trying to perform function where you can only set up objects, primitives, etc

@Override
public void onCreate(Bundle savedInstanceState) {
....
}

rather than this:

SQLiteDatabase db;

@Override
public void onCreate(Bundle savedInstanceState) {
db = openOrCreateDatabase("DB", MODE_PRIVATE, null);
....
}
Chris Klingler
  • 5,258
  • 2
  • 37
  • 43
8

For me, my package string in AndroidManifest.xml was incorrect (copied from a tutorial). Make sure the package string in this file is the same as where your main activity is, e.g.

 package="com.example.app"

An easy way to do this is to open the AndroidManifest.xml file in the "Manifest" tab, and type it in the text box next to Package, or use the Browse button.

Also, the package string for my activity was wrong, e.g.

<activity android:name="com.example.app.MainActivity" android:label="@string/app_name">

I (stupidly) got the same error weeks later when I renamed my package name. If you do this, make sure you update the AndroidManifest.xml file too.

Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
satyrFrost
  • 393
  • 8
  • 17
  • 1
    I made the same mistake in Flutter. I changed the package name, but only in the AnroidManifest.xml files. You have to change it at multiple other locations across the project. Just Ctrl+Shift+F your old package name and change it everywhere. – Zciurus Nov 04 '22 at 10:55
7

I got rid of this problem by deleting the Bin and Gen folder from project(which automatically come back when the project will build) and then cleaning the project from ->Menu -> Project -> clean.

Thanks.

Jagdeep Singh
  • 1,200
  • 1
  • 16
  • 34
7

Simply Clean your working project or restart eclipse. Then run your project. it will work.

Emran Hamza
  • 3,829
  • 1
  • 24
  • 20
  • So many times this has happened... Now it can't resolve an IADL file, which is probably the root of the problem and eclipse was compiling based on old files... sooo remember to clean, restart, and compile kids... – Hunter-Orionnoir Oct 07 '15 at 22:37
6

For me it was different from any of the above,

The activity was declared as abstract, That is why giving the error. Once it removed it worked.

Earlier

     public abstract class SampleActivity extends AppcompatActivity{
     }

After removal

     public class SampleActivity extends AppcompatActivity{
     }
Thriveni
  • 742
  • 9
  • 11
6

In my case I haven't set the setContentView(R.layout.main);

If you create a new class do not foget to set this in on onCreate(Bundle savedInstanceState) method.

I have done this stupid mistake several times.

saji159
  • 328
  • 7
  • 14
5

Ok, I am fairly experienced on the iPhone but new to android. I got this issue when I had:

Button infoButton = (Button)findViewById(R.id.InfoButton);

this line of code above:

@Override
public void onCreate (Bundle savedInstanceState) {
}

May be I am sleep deprived :P

iSee
  • 604
  • 1
  • 14
  • 31
5

In my case, I was trying to initialize the components(UI) even before the onCreate is called for the Activity.

Make sure your UI components are initialized/linked in the onCreate method after setContentView

NB: This is my first mistake while learning Android programming.

Anup H
  • 549
  • 7
  • 10
5

Make sure MainActivity is not "abstract".

abstract class MainActivity : AppCompatActivity()

just remove the abstract

class MainActivity : AppCompatActivity() {
Amit Singh
  • 609
  • 7
  • 7
3

I recently encountered this with fresh re-install of Eclipse. Turns out my Compiler compliance level was set to Java 1.7 and project required 1.6.

In Eclipse: Project -> Properties -> Java Compiler -> JDK Compliance

J.G.Sebring
  • 5,934
  • 1
  • 31
  • 42
3

Whow are there lots of ways to get this error!.

I will add another given none of the others either applied or were the cause.

I forgot the 'public' in the declaration of my activity class! It was package private.

I had been writing so much Bluetooth code that habit caused me to create an activity class that was package private.

Says something about the obscurity of this error message.

Brian Reinhold
  • 2,313
  • 3
  • 27
  • 46
2

Got this problem, and fixed it by setting the "launch mode" property of the activity.

Zorglube
  • 21
  • 1
2

Another reason of this problem may be a missing library.

Go to Properties -> Android and check that you add the libraries correctly

ayalcinkaya
  • 3,303
  • 29
  • 25
  • 1
    Also check that the libraries added have a tick mark against them in Project Properties -> Java Build Path -> Order and Export – Phil Haigh Jun 23 '13 at 18:18
2

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...).

2

This can happen if your activity class is inside a default package. I fixed it by moving the activity class to a new package. and changing the manifest.xml

before

activity android:name=".MainActivity" 

after

activity android:name="new_package.MainActivity"
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
Satthy
  • 109
  • 1
  • 3
  • 10
2

As suggested by djjeck in comment in this answer I missed to put public modifier for my class.

It should be

public class MyActivity extends AppCompatActivity {

It may help some like me.

Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
2

In my case I forget to make MainActivity class as public.

Other solutions:

  1. Add activity in Manifest file

  2. Verify all access modifiers

manoj kiran
  • 79
  • 1
  • 4
1

Most the time If Activity name changed reflected all over the project except the AndroidManifest.xml file.

You just need to Add the name in manifest manually.

<activity
android:name="Activity_Class_Name"
android:label="@string/app_name">
</activity>
Anand Dwivedi
  • 1,452
  • 13
  • 23
Swapnil Kotwal
  • 5,418
  • 6
  • 48
  • 92
1

This error also occurs when you use of ActionBarActivity but assigned a non AppCompat style.

To fix this just set your apps style parent to an Theme.AppCompat one like this.:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
    <!-- Customize your theme here. -->
</style>
Ostkontentitan
  • 6,930
  • 5
  • 53
  • 71
1

Right click on project > properties > android > and try with different version of the android earlier i was doing with android 4.4 then i changed to android 4.3 and it worked !

nikhilgotan
  • 112
  • 1
  • 7
1

I had the same issue (Unable to instantiate Activity) :

FIRST reason :

I was accessing

Camera mCamera;
Camera.Parameters params = mCamera.getParameters();

before

mCamera = Camera.open();

So right way of doing is, open the camera first and then access parameters.

SECOND reason : Declare your activity in the manifest file

<activity android:name=".activities.MainActivity"/>

THIRD reason : Declare Camera permission in your manifest file.

<uses-feature android:name="android.hardware.Camera"></uses-feature>
<uses-permission android:name="android.permission.CAMERA" />

Hope this helps

Sdembla
  • 1,629
  • 13
  • 13
1

In my case, I was trying to embed the Facebook SDK and I was having the wrong Application ID; thus the error was popping up. In your manifest file, you should have the proper meta data:

<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id" />
Menelaos Kotsollaris
  • 5,776
  • 9
  • 54
  • 68
1

If you have Android Studio 2.3.3 and Android Studio 3.0.0 installed, then switching between the two programs for development will cause this error. This is because there exist situations where classes supported in one program is not supported by the other and vice versa. It is important to maintain consistency in which version of Android Studio is being used to develop a project.

hexicle
  • 2,121
  • 2
  • 24
  • 31
1

I have tried all above solution but nothing work for me. after I have just add extend activity instead of AppCompatActivity and working fine.

used

public class MainActivity extends Activity  

instead of

public class MainActivity extends AppCompatActivity 

i dont know what real issue was that.

Sagar Jethva
  • 986
  • 12
  • 26
1

This also happens when you type the wrong applicationId in the build.gradle file.

For eg. Your application ID is com.example.myapp.app

And you wrote build.gradle file like this:

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "com.example.myapp"
        //this will make your app crash so write exact applicationId as it is
        minSdkVersion 8
        targetSdkVersion 8
    }
Ritesh Khandekar
  • 3,885
  • 3
  • 15
  • 30
1

My problem was missing package declaration in the beginning of the activity source file:

package com.my.package
apaluk
  • 361
  • 3
  • 7
1

This happens to me fairly frequently when using the NDK. I found that it is necessary for me to do a "Clean" in Eclipse after every time I do a ndk-build. Hope it helps anyone :)

kizzx2
  • 18,775
  • 14
  • 76
  • 83
0

This happened to me when I tried to run an Activity on 2.2 that used imports from Honeycomb not available in older versions of Android and not included in the v4 support package either.

pqn
  • 1,522
  • 3
  • 22
  • 33
0

Your New Activity add AndroidManifest.xml like ".NewActivity"

 </activity>        
    <activity android:name=".NewActivity" />
NagarjunaReddy
  • 8,621
  • 10
  • 63
  • 98
0

You should make sure that all the activities in your application are properly defined in the AndroidManifest.xml file.

Also, if they start with a path make sure this path exists.

E.g. this will not work:

<activity android:name=".com.example.android.networkusage.NetworkActivity" android:label="@string/app_name" >

If your application's name is com.example.networkusage (note the missing .android.)

Or better yet, don't include paths when defining an activity inside the manifest. Only put their name (with a dot in front of it).

<activity android:name=".NetworkActivity" android:label="@string/app_name" >
Dzhuneyt
  • 8,437
  • 14
  • 64
  • 118
0

you may have some errors in your fields like

public class Search_costumer extends Activity {

// you may have some errors like this 
int x =3/0;
// I have error in initializing this variable too 
 MySimpleOnGestureListener mySimpleOnGestureListener =new MySimpleOnGestureListener();

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
}
}

so my advice is to perform all initialization in onCreate method not directly in your fields

solve like that

public class Search_costumer extends Activity {

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

// you may have some errors like this 
int x =3/0;
// I have error in initializing this variable too 
 MySimpleOnGestureListener mySimpleOnGestureListener =new MySimpleOnGestureListener();
}
}
0

This can also happen if you have two projects with the same package name and you ran one of them before the other so I guess it confuses Eclipse. For me this happened when I copied an Android tutorial in eclipse in the same workspace and left the package names the same but added ActionBarSherlock (ABS) to it, e.g.

 package="com.example.android.whatever"

So in my AndroidManifest.xml file, both the tutorial project that was in my workspace and the copied tutorial project both had as the package string:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.whatever"

    ....
</manifest>

Just change the package name by refactoring (In eclipse, right click on package in Package Explorer tab > select Refractor > select Rename...) and then changing it in your AndroidManifest.xml file and anywhere else you need to (e.g. like all your .java files)

nommer
  • 2,730
  • 3
  • 29
  • 44
0

Also if you're retrieving something from intent of another activity using getIntent in the current activity and setting those retrieved data before/above onCreate then this exception is thrown.

For eg. if i retrieve a string like this

final String slot1 = getIntent().getExtras().getString("Slot1");

and put this line of code before/above onCreate then this exception is thrown.

Sash_KP
  • 5,551
  • 2
  • 25
  • 34
0

In my case I had two version of "android-support-v4.jar" references in the project. After resolving this (removed additional/incorrect reference) solved the issue.

ryaliscs
  • 123
  • 9
0

In my case, I had to add a new Android 6.0 Library by going to Window >> Preferences >> Java >> Build Path >> User Libraries >> Add Library >> User Library >> New

Name it Android 6.0, then click OK. After that, add the android.jar file that you can find in "sdklocation/platforms/android-23/android.jar"

Make sure you do all this in the Android project.

Eames
  • 321
  • 2
  • 19
0

Sometimes isMinifyEnabled = false may help (make sure you clean the project before launch)

Igor
  • 2,039
  • 23
  • 27
0

Don't do like this

ImageView imageView=findViewById(R.id.imageView);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

Do it like this

ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imageView=findViewById(R.id.imageView);
Karthik Kompelli
  • 2,104
  • 1
  • 19
  • 22
0

For flutter applications. Make sure you run flutter clean and flutter pub get after you change the package name.

Fredrick
  • 71
  • 6
0

Also check that the package name is the same in android/app/src/main/kotlin/com/.../MainActivity.kt package com.xxxx.xxxx and AndroidManifest.xml package="com.xxxx.xxxx"

ramzieus
  • 138
  • 11
0

I faced the same issue while running my Flutter application in debug mode. I managed to solve it via amending the below changes into MainActivity.kt

package com.example.yourappname

import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity() {
    // You can add any necessary code or configurations here
}