2012-07-14 148 views
0

我跟随了动态壁纸教程,并看到其他开发人员的代码,他们中的大多数都比我仅使用相同的代码稍微修改得更远。我只想显示带有一系列图像的动态壁纸,但它在启动时崩溃。这里是我的代码堆栈跟踪和清单。我需要为此设置一个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

你在你的项目有任何活动课?请发布你的清单和壁纸xml。 – 2012-07-14 06:41:04

+1

某处“LiveActivity”被当作“Activity”对象处理,它实际上是一个“WallpaperService”对象(它不会扩展Activity)。这可能在TJ提到的清单中,或者另一个'* .java'文件中。 – Eric 2012-07-14 07:04:56

+0

啊,我可能已经看到我的清单中列出的一项活动,包括现场活动,当我回家并让你们知道的时候,它就会崩溃,谢谢! – user1493891 2012-07-14 16:33:22

回答