AI编程生活评测

Android运行错误:No Launcher activity found! The launch will only sync the application package on the device!

编程笔记 / 2015-06-27 / 2 min
在Eclipse环境下运行Android应用提示错误:
No Launcher activity found!
The launch will only sync the application package on the device!
此错误一般发生说明在你的manifest.xml中没有为action设置android.intent.action.MAIN指定的activity,也就是没有告诉程序哪个activity作为主activity来启动,就会出现前面错误提示。 正确manifest.xml文件实例:
<activity>
      android:name="mars.mp3player.Mp3ListActivity"
      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></activity> 内部的 intent-filter 就是用来指定这个 activity 为程序入口启动的 activity ,加入这个 intent-filter 就可以正常运行了! 但是可悲的是我在加入这段 intent-filter 之后还是出现错误,原来在创建程序的时候,我并没有创建布局文件,导致 layout 未指定。具体就在上面代码中提到的 android:name="mars.mp3player.Mp3ListActivity" 这个 Mp3ListActivity 文件中的这段代码中:
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}
setContentView(R.layout.main) ;这句代买没有加上,所以无法找到布局文件,就会提示错误,这里的 main 上面那个 Activity 启动需要加载的布局文件;至此,完成运行。