[转载](2)Android程序的Unable to instantiate activity ComponentInfo异常_iechenyb_新浪博客

[转载](2)Android程序的Unable to instantiate activity ComponentInfo异常_iechenyb_新浪博客.

今天写程序,一直被java.lang.RuntimeException: Unable to instantiate activity ComponentInfo异常困扰,查看LogCat也没有我自己写的文件异常抛出,一直不明白是怎么回事,后来上网搜了搜,看到大家都觉的是这些可能:

It is a problem of your Intent.

Please add your Activity in your AndroidManifest.xml.

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

意思是已经写好了Activity这个类并且想通过Intent传递以便切换Activity,但是并没有在Manifest.xml文件中添加这个Activity,导致不能实例化Activity。

In my case I forgot to add the google maps library

uses-library android:name=”com.google.android.maps”

it may be useful to someone: http://developer.android.com/resources/tutorials/views/hello-mapview.html

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

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

这个意思是类的路径写的不对(比如说一个Activyty在com.j2men包里,一个在com.j2men.ui包里,这时在Manifest中默认 定义的package是com.j2men,如果你写一个新的Activity进去时,就得在.类名前面再加上.ui才行)或者在 Manifest.xml中android:name=”.类名”这里的类名前忘加了那个点。

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.

这个人说的意思是如果你把默认intent的Activity定义为抽象类的话,也会弹出这种错误。

如果这三种情况你都考虑到了,并且也做了相应的修改后依然抛出这个异常,还有一招:你去看看你要打开的那个Activity类里是否同时存在构造函数和onCreate()方法,如果是的话请把构造函数删除试试。把初始化放在onCreate()方法里进行。

联网程序如果获取不到数据的时候,检查一下Manifest.xml里面是否忘记添加了网络权限的语句:

< uses-permission android:name=”android.permission.INTERNET” />

赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏