2012-06-27 62 views
1

所以我知道「錯誤膨脹類」通常是因爲缺少或格式錯誤的構造函數......但是我讓eclipse生成它們,它們看起來與我見過的其他示例相似。該錯誤指向我的XML文件,我試圖將自定義類添加到我的佈局。Android - 子類ImageView - 錯誤膨脹類

我假設我必須聲明擴展ImageView一些如何但我不知道如何這樣做。任何幫助,你可以提供將不勝感激。

錯誤

06-27 09:12:18.655: D/AndroidRuntime(1487): Shutting down VM 
06-27 09:12:18.655: W/dalvikvm(1487): threadid=1: thread exiting with uncaught exception (group=0x409c01f8) 
06-27 09:12:18.675: I/dalvikvm(1487): threadid=3: reacting to signal 3 
06-27 09:12:18.715: I/dalvikvm(1487): Wrote stack traces to '/data/anr/traces.txt' 
06-27 09:12:18.755: E/AndroidRuntime(1487): FATAL EXCEPTION: main 
06-27 09:12:18.755: E/AndroidRuntime(1487): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mytheral.android.AnimationTimer/com.mytheral.android.AnimationTimer.AnimationTimerActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class AnimationImageView 
06-27 09:12:18.755: E/AndroidRuntime(1487):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 
06-27 09:12:18.755: E/AndroidRuntime(1487):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
06-27 09:12:18.755: E/AndroidRuntime(1487):  at android.app.ActivityThread.access$600(ActivityThread.java:123) 
06-27 09:12:18.755: E/AndroidRuntime(1487):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
06-27 09:12:18.755: E/AndroidRuntime(1487):  at android.os.Handler.dispatchMessage(Handler.java:99) 
06-27 09:12:18.755: E/AndroidRuntime(1487):  at android.os.Looper.loop(Looper.java:137) 
06-27 09:12:18.755: E/AndroidRuntime(1487):  at android.app.ActivityThread.main(ActivityThread.java:4424) 
06-27 09:12:18.755: E/AndroidRuntime(1487):  at java.lang.reflect.Method.invokeNative(Native Method) 
06-27 09:12:18.755: E/AndroidRuntime(1487):  at java.lang.reflect.Method.invoke(Method.java:511) 
06-27 09:12:18.755: E/AndroidRuntime(1487):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
06-27 09:12:18.755: E/AndroidRuntime(1487):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
06-27 09:12:18.755: E/AndroidRuntime(1487):  at dalvik.system.NativeStart.main(Native Method) 
06-27 09:12:18.755: E/AndroidRuntime(1487): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class AnimationImageView 
06-27 09:12:18.755: E/AndroidRuntime(1487):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691) 
06-27 09:12:18.755: E/AndroidRuntime(1487):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:739) 
06-27 09:12:18.755: E/AndroidRuntime(1487):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
06-27 09:12:18.755: E/AndroidRuntime(1487):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
06-27 09:12:18.755: E/AndroidRuntime(1487):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
06-27 09:12:18.755: E/AndroidRuntime(1487):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251) 
06-27 09:12:18.755: E/AndroidRuntime(1487):  at android.app.Activity.setContentView(Activity.java:1835) 
06-27 09:12:18.755: E/AndroidRuntime(1487):  at com.mytheral.android.AnimationTimer.AnimationTimerActivity.onCreate(AnimationTimerActivity.java:15) 
06-27 09:12:18.755: E/AndroidRuntime(1487):  at android.app.Activity.performCreate(Activity.java:4465) 
06-27 09:12:18.755: E/AndroidRuntime(1487):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 
06-27 09:12:18.755: E/AndroidRuntime(1487):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 
06-27 09:12:18.755: E/AndroidRuntime(1487):  ... 11 more 
06-27 09:12:18.755: E/AndroidRuntime(1487): Caused by: java.lang.ClassNotFoundException: android.view.AnimationImageView 
06-27 09:12:18.755: E/AndroidRuntime(1487):  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61) 
06-27 09:12:18.755: E/AndroidRuntime(1487):  at java.lang.ClassLoader.loadClass(ClassLoader.java:501) 
06-27 09:12:18.755: E/AndroidRuntime(1487):  at java.lang.ClassLoader.loadClass(ClassLoader.java:461) 
06-27 09:12:18.755: E/AndroidRuntime(1487):  at android.view.LayoutInflater.createView(LayoutInflater.java:552) 
06-27 09:12:18.755: E/AndroidRuntime(1487):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:636) 
06-27 09:12:18.755: E/AndroidRuntime(1487):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66) 
06-27 09:12:18.755: E/AndroidRuntime(1487):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653) 
06-27 09:12:18.755: E/AndroidRuntime(1487):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678) 
06-27 09:12:18.755: E/AndroidRuntime(1487):  ... 21 more 

XML文件

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <AnimationImageView 
     android:id="@+id/progress_bar" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_launcher" /> 

</LinearLayout> 

AnimationTimerActivity

package com.mytheral.android.AnimationTimer; 
import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 

public class AnimationTimerActivity extends Activity { 
    /** Called when the activity is first created. */ 

    private static final String TAG = "AnimationTimer"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     Log.v(TAG, "onCreate"); 
     AnimationImageView image= (AnimationImageView) findViewById(R.id.progress_bar); 

     image.StartAnimation(); 
    } 
} 

AnimationImageView

package com.mytheral.android.AnimationTimer; 
import java.util.Timer; 
import java.util.TimerTask; 
import android.content.Context; 
import android.net.Uri; 
import android.os.Handler; 
import android.os.Message; 
import android.util.AttributeSet; 
import android.util.Log; 
import android.widget.ImageView; 

public class AnimationImageView extends ImageView { 

    private static final String TAG = "AnimationImageView"; 

    private Timer imgTimer; 
    private int currentFrame = 0; 
    private int maxFrame = 269; //Testing Value 
    private String fileName = "green_flag_"; //Testing Value 

    public int getCurrentFrame() { 
     return currentFrame; 
    } 

    public void setCurrentFrame(int currentFrame) { 
     this.currentFrame = currentFrame; 
    } 

    public int getMaxFrame() { 
     return maxFrame; 
    } 

    public void setMaxFrame(int maxFrame) { 
     this.maxFrame = maxFrame; 
    } 

    public String getFileName() { 
     return fileName; 
    } 

    public void setFileName(String fileName) { 
     this.fileName = fileName; 
    } 

    public AnimationImageView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    public AnimationImageView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public AnimationImageView(Context context) { 
     super(context); 
    } 

    public void StartAnimation() { 

     Log.v(TAG, "onCreate"); 

     currentFrame = 0; 
     maxFrame = 269; 
     imgTimer = new Timer(); 

     try { 
      imgTimer.scheduleAtFixedRate(new TimerTask() { 

       @Override 
       public void run() { 

        imgUpdateHandler.sendEmptyMessage(0); 

       }}, 0, 67); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    private Handler imgUpdateHandler = new Handler() { 
     @Override 
     public void handleMessage(Message msg) { 
      Log.v(TAG, "imgUpdateHandler: currentFrame: " + currentFrame); 

      Uri imgUri; 

      if (currentFrame <= maxFrame) 
      { 
       int i = currentFrame; 

       imgUri = Uri.parse("android.resource://com.mytheral.android.AnimationTimer/drawable/" + fileName + String.format("%04d", i)); 

       //Uncomment if images don't cycle... Prior Android work around 
       //image.setImageURI(null); 

       try { 
        setImageURI(imgUri); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 

       currentFrame++; 
      } 

      if (currentFrame > maxFrame) 
      { 
       Log.v(TAG, "imgUpdateHandler: stopAnimation"); 

       imgTimer.cancel(); 
       imgTimer.purge(); 
      } 
     }  
    }; 
} 

回答

5

代替這種使用

<AnimationImageView 

使用

<com.mytheral.android.AnimationTimer.AnimationImageView 
    android:id="@+id/progress_bar" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/ic_launcher" /> 

<view class="com.mytheral.android.AnimationTimer.AnimationImageView" 
     android:id="@+id/progress_bar" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_launcher" /> 

/>

相關問題