2011-05-23 22 views
1

我試圖實現的動畫繪製具有以下...加載器動畫空指針?

<?xml version="1.0" encoding="UTF-8"?> 
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
android:oneshot="false" > 
<item android:drawable="@drawable/load_0000" android:duration="50" /> 
<item android:drawable="@drawable/load_0001" android:duration="50" /> 
<item android:drawable="@drawable/load_0002" android:duration="50" /> 
<item android:drawable="@drawable/load_0003" android:duration="50" /> 
<item android:drawable="@drawable/load_0004" android:duration="50" /> 
<item android:drawable="@drawable/load_0005" android:duration="50" /> 
<item android:drawable="@drawable/load_0006" android:duration="50" /> 
<item android:drawable="@drawable/load_0007" android:duration="50" /> 
<item android:drawable="@drawable/load_0008" android:duration="50" /> 
<item android:drawable="@drawable/load_0009" android:duration="50" /> 
</animation-list> 

然而,當我執行下面的代碼我得到後仰空指針...

  ImageView img = (ImageView)findViewById(R.id.load_bar); 
    img.setBackgroundResource(R.drawable.loader_animation); //null here. 

爲什麼? loader_animation是文件名,它位於繪製/

回答

2

的問題是,img爲空:

有沒有在XML中查看你已經裝載setContentView(或膨脹,並添加到內容查看),其有編號load_bar

請注意,只是在某些XML中感到興奮,或稍後加載它不起作用。 findViewById查找當前視圖!

如果還沒有加載,請使用inflater