我关注了动态壁纸的教程,并且已经看到了其他开发人员的代码,并且大多数都比我使用相同的代码稍微修改得更远。我只想显示带有一系列图像的动态壁纸,但它在启动时崩溃。这是我的代码,包含堆栈跟踪和清单。我需要为此设置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,但我不知道如何解决它。有什么想法吗?
答案 0 :(得分:0)
这里的问题是您的LiveActivity类无法作为Android应用程序启动,因为它来自WallpaperService而非活动。
您可以在Android开发者网站上获得有关服务和活动的更多信息
http://developer.android.com/
我自己一直在线学习教程,以下教程对我有用:
http://learnandroideasily.blogspot.kr/2013/07/android-livewallpaer-tutorial.html