我遵循了一个关于动态壁纸的教程,并且看到了其他开发人员在上面编写的代码,并且大多数都比我使用相同的代码更进一步。我只想显示带有一系列图像的动态壁纸,但它在启动时崩溃。这是我的带有堆栈跟踪和清单的代码。我需要为此设置一个xml布局吗?
package com.warzone.livewallpaper;
import android.os.Handler;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.SurfaceHolder;
import android.service.wallpaper.WallpaperService;
public class LiveActivity extends WallpaperService {
public void onCreate() {
super.onCreate();
}
public void onDestroy() {
super.onDestroy();
}
public Engine onCreateEngine() {
return new WarEngine();
}
class WarEngine extends Engine {
private final Handler handler = new Handler();
private final Runnable drawRunner = new Runnable() {
@Override
public void run() {
draw();
}
};
private boolean visible = true;
public Bitmap image1, image2, image3;
WarEngine() {
image1 = BitmapFactory.decodeResource(getResources(),
R.drawable.wz0351);
image2 = BitmapFactory.decodeResource(getResources(),
R.drawable.wz0375);
image3 = BitmapFactory.decodeResource(getResources(),
R.drawable.wz0391);
}
public void onCreate(SurfaceHolder surfaceHolder) {
super.onCreate(surfaceHolder);
}
@Override
public void onVisibilityChanged(boolean visible) {
this.visible = visible;
if (visible) {
handler.post(drawRunner);
} else {
handler.removeCallbacks(drawRunner);
}
}
@Override
public void onSurfaceDestroyed(SurfaceHolder holder) {
super.onSurfaceDestroyed(holder);
this.visible = false;
handler.removeCallbacks(drawRunner);
}
public void onOffsetsChanged(float xOffset, float yOffset, float xStep,
float yStep, int xPixels, int yPixels) {
draw();
}
void draw() {
final SurfaceHolder holder = getSurfaceHolder();
Canvas c = null;
try {
c = holder.lockCanvas();
if (c != null) {
c.drawBitmap(image1, 0, 0, null);
c.drawBitmap(image2, 0, 0, null);
c.drawBitmap(image3, 0, 0, null);
}
} finally {
if (c != null)
holder.unlockCanvasAndPost(c);
}
handler.removeCallbacks(drawRunner);
if (visible)
{
handler.postDelayed(drawRunner, 1000); // delay 1 sec
}
}
}
}
*07-14 02:10:50.524: E/AndroidRuntime(622): FATAL EXCEPTION: main
*07-14 02:10:50.524: E/AndroidRuntime(622): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.warzone.livewallpaper/com.warzone.livewallpaper.LiveActivity}:java.lang.ClassCastException: com.warzone.livewallpaper.LiveActivity cannot be cast to android.app.Activity
*07-14 02:10:50.524: E/AndroidRuntime(622): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
*07-14 02:10:50.524: E/AndroidRuntime(622): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
*07-14 02:10:50.524: E/AndroidRuntime(622): at android.app.ActivityThread.access$600(ActivityThread.java:130)
*07-14 02:10:50.524: E/AndroidRuntime(622): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
*07-14 02:10:50.524: E/AndroidRuntime(622): at android.os.Handler.dispatchMessage(Handler.java:99)
*07-14 02:10:50.524: E/AndroidRuntime(622): at android.os.Looper.loop(Looper.java:137)
*07-14 02:10:50.524: E/AndroidRuntime(622): at android.app.ActivityThread.main(ActivityThread.java:4745)
*07-14 02:10:50.524: E/AndroidRuntime(622): at java.lang.reflect.Method.invokeNative(Native Method)
*07-14 02:10:50.524: E/AndroidRuntime(622): at java.lang.reflect.Method.invoke(Method.java:511)
*07-14 02:10:50.524: E/AndroidRuntime(622): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
*07-14 02:10:50.524: E/AndroidRuntime(622): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
*07-14 02:10:50.524: E/AndroidRuntime(622): at dalvik.system.NativeStart.main(NativeMethod)
*07-14 02:10:50.524: E/AndroidRuntime(622): Caused by: java.lang.ClassCastException: com.warzone.livewallpaper.LiveActivity cannot be cast to android.app.Activity
*07-14 02:10:50.524: E/AndroidRuntime(622): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
*07-14 02:10:50.524: E/AndroidRuntime(622): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
*07-14 02:10:50.524: E/AndroidRuntime(622): ... 11 more
我的清单文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.warzone.livewallpaper"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".LiveActivity"
android:label="@string/title_activity_live" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="@dimen/padding_medium"
android:text="@string/hello_world"
tools:context=".LiveActivity" />
我注意到 ClassCastException 但我不知道如何解决它。有任何想法吗?