Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo

Tags:

android

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.


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

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.


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"/>

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.


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.