使用动画和线程

时间:2014-01-16 09:01:55

标签: android multithreading animation

我想创建一个动画。 为什么我会收到此错误?

  

01-16 12:09:33.388:E / AndroidRuntime(895):致命异常:线程-10

     

01-16 12: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):at   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):at java.lang.Thread.run(Thread.java:1020)   01-16 12:15:11.178:E / AndroidRuntime(990):致命异常:线程-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):at   com.example.pages.Comments.access $ 0(Comments.java:35)01-16   12:15:11.178:E / AndroidRuntime(990):at   com.example.pages.Comments $ 1.run(Comments.java:23)01-16 12:15:11.178:   E / AndroidRuntime(990):在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>

1 个答案:

答案 0 :(得分:0)

您无法在新帖子中更新UIUI必须在UI帖子中更新

您尝试更新initialImageViewSms();中不在UI线程

中的用户界面

您可以使用 runOnUiThread 处理程序 AsyncTask 类在UI线程中运行或从线程移出您的方法,因为我没有看到任何其他代码。