尝试使用两个类运行动态壁纸项目: 1. LiveWallpaper 2. LiveWallpaperSettings 我在清单中添加了两个名称,但我不知道为什么我在这里得到这个classcast异常。我也在添加清单代码:
12-13 22:41:48.861: W/dalvikvm(3599): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
12-13 22:41:48.871: E/AndroidRuntime(3599): FATAL EXCEPTION: main
12-13 22:41:48.871: E/AndroidRuntime(3599): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{ca.jvsh.livewallpaper/ca.jvsh.livewallpaper.LiveWallpaper}: java.lang.ClassCastException: ca.jvsh.livewallpaper.LiveWallpaper cannot be cast to android.app.Activity
12-13 22:41:48.871: E/AndroidRuntime(3599): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1879)
12-13 22:41:48.871: E/AndroidRuntime(3599): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
12-13 22:41:48.871: E/AndroidRuntime(3599): at android.app.ActivityThread.access$600(ActivityThread.java:122)
12-13 22:41:48.871: E/AndroidRuntime(3599): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
12-13 22:41:48.871: E/AndroidRuntime(3599): at android.os.Handler.dispatchMessage(Handler.java:99)
12-13 22:41:48.871: E/AndroidRuntime(3599): at android.os.Looper.loop(Looper.java:137)
12-13 22:41:48.871: E/AndroidRuntime(3599): at android.app.ActivityThread.main(ActivityThread.java:4340)
12-13 22:41:48.871: E/AndroidRuntime(3599): at java.lang.reflect.Method.invokeNative(Native Method)
12-13 22:41:48.871: E/AndroidRuntime(3599): at java.lang.reflect.Method.invoke(Method.java:511)
12-13 22:41:48.871: E/AndroidRuntime(3599): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-13 22:41:48.871: E/AndroidRuntime(3599): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-13 22:41:48.871: E/AndroidRuntime(3599): at dalvik.system.NativeStart.main(Native Method)
12-13 22:41:48.871: E/AndroidRuntime(3599): Caused by: java.lang.ClassCastException: ca.jvsh.livewallpaper.LiveWallpaper cannot be cast to android.app.Activity
12-13 22:41:48.871: E/AndroidRuntime(3599): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
12-13 22:41:48.871: E/AndroidRuntime(3599): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1870)
12-13 22:41:48.871: E/AndroidRuntime(3599): ... 11 more
12-13 22:41:51.891: I/Process(3599): Sending signal. PID: 3599 SIG: 9
* 清单代码 *
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ca.jvsh.livewallpaper"
android:versionCode="1"
android:versionName="1.0.20100908.1" >
<uses-sdk android:minSdkVersion="7" />
<uses-feature android:name="android.software.live_wallpaper" />
<application
android:icon="@drawable/icon"
android:label="@string/app_name"
android:permission="android.permission.BIND_WALLPAPER" >
<service
android:name=".LiveWallpaper"
android:icon="@drawable/icon"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.service.wallpaper.WallpaperService" />
</intent-filter>
<meta-data
android:name="android.service.wallpaper"
android:resource="@layout/livewallpaper" />
</service>
<activity
android:name=".LiveWallpaperSettings"
android:exported="true"
android:icon="@drawable/icon"
android:label="@string/livewallpaper_settings"
android:theme="@android:style/Theme.Light.WallpaperSettings" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:3)
您在清单中声明具有相同名称的服务和活动,因此请为每个看法使用不同的名称
<service
android:name=".LiveWallpaper" <<<-----here
android:icon="@drawable/icon"
android:label="@string/app_name" >
<intent-filter>
<action android:name=
"android.service.wallpaper.WallpaperService" />
</intent-filter>
<meta-data
android:name="android.service.wallpaper"
android:resource="@layout/livewallpaper" />
</service>
<activity
android:name=".LiveWallpaper" <<<-----here
android:exported="true"
android:icon="@drawable/icon"
android:label="@string/livewallpaper_settings"
并在代码部分,如果您正在启动LiveWallpaper服务,那么将其作为LiveWallpaperSettings启动:
Intent intent=new Intent(getApplicationContext(),LiveWallpaper.class);
startService(intent);