2014-01-16 66 views
0

我想創建一個動畫。 爲什麼我得到這個錯誤?使用動畫和線程

12月1日至16日:09:33.388:E/AndroidRuntime(895):致命異常:螺紋10

12月1日至16日:09:33.388:E/AndroidRuntime(895):

java.lang.NullPointerException 01-16 12:09:33.388: E/AndroidRuntime(895):at com.example.pages.Comments.initialImageViewSms(Comments.java:38)01-16 12:09: 33.388:E/AndroidRuntime(895):在 com.example.pages.Comments.access $ 0(Comments.java:36)01-16 12:09:33.388:E/AndroidRuntime(895):at com.example.pages.Comments $ 1.run(Comments.java:24)01-16 12:09:33.388: E/AndroidRuntime(895):at java.lang.Thread.run(Thread.java:1020) 01-16 12:09:45.587:E/AndroidRuntime(906):致命例外:線程10 01-16 12:09:45.587:E/AndroidRuntime(906): java.lang.NullPointerException 01-16 12 :09:45.587: E/AndroidRuntime(906):at com.example.pages.Comments.initialImageViewSms(Comments.java:38)01-16 12:09:45.587:E/AndroidRuntime(906):at com.example.pages.Comments.access $ 0(Comments.java:36)01-16 12:09:45.587:E/AndroidRuntime(906):at com.example.pages.Comments $ 1.run(Comments.java :24)01-16 12:09:45.587: E/AndroidRuntime(906):a t java.lang.Thread.run(Thread.java:1020) 01-16 12:15:11.178:E/AndroidRuntime(990):致命例外:Thread-10 01-16 12:15:11.178:E/AndroidRuntime(990): java.lang.NullPointerException 01-16 12:15:11.178: E/AndroidRuntime(990):at com.example.pages.Comments.initialImageViewSms(Comments.java:37)01-16 12:15:11.178:E/AndroidRuntime(990):在 com.example.pages.Comments.access $ 0(Comments.java:35)01-16 12:15:11.178:E/AndroidRuntime(990):at E/AndroidRuntime(990):at java.lang.Thread.run(Thread.java:1020)

BaseFragment:

package com.example.pages; 

import android.support.v4.app.Fragment; 

public class BaseFragment extends Fragment { 

} 

評論:

package com.example.pages; 

import android.graphics.drawable.AnimationDrawable; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
import android.widget.Toast; 

public class Comments extends BaseFragment { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    new Thread(new Runnable() { 

     @Override 
     public void run() { 
      // TODO Auto-generated method stub 
      initialImageViewSms(); 
     } 
    }).start(); 

} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 
    View view=inflater.inflate(R.layout.comments,container,false); 
    return view; 
} 

private void initialImageViewSms(){ 

    ImageView imageview_sendsms = (ImageView)getView().findViewById(R.id.imagebutton_send_sms); 
    imageview_sendsms.setBackgroundResource(R.drawable.send_sms_anim); 
    AnimationDrawable animdrawable = (AnimationDrawable)imageview_sendsms.getBackground(); 
    animdrawable.start(); 
} 
} 

send_sms_anim.xml:

<?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/send_sms_one" 
android:duration="100"/> 
<item 
android:drawable="@drawable/send_sms_two" 
android:duration="100"/> 

</animation-list> 
+0

檢查此[http://www.androidhive。info/2013/06/android-working-with-xml-animations /]爲你的問題,可能是這樣解決你的問題 –

+1

爲什麼你從一個線程開始你的動畫? – njzk2

回答

0

你不能在UI線程更新UI在新的線程,UI必須更新

您嘗試在initialImageViewSms();更新用戶界面,是不是在UI線程

可以使用runOnUiThread處理器的AsyncTask類在UI線程中運行或線程搬出你的方法,因爲我不看到其中的任何其他代碼。

+0

TXN沙炎,怎麼樣? :( – user3103823

+0

你想要什麼?你想爲你的'ImageView'開始動畫嗎? –

+0

是的,我想開始一個圖像視圖的動畫! – user3103823