【问题标题】:while playing gif animation some times getting nullpointer exception在播放 gif 动画时有时会出现空指针异常
【发布时间】:2026-01-07 08:15:02
【问题描述】:

我正在我的应用程序中玩gif animation。它工作得很好,但如果我玩了很多次相同的animation,它会在电影对象中抛出nullpointer exception

关于这个问题的任何建议。下面是我的代码

SampleView.class:

 class SampleView extends View {
           java.io.InputStream is;


            private Movie mMovie;
            private long mMovieStart;

            private  byte[] streamToBytes(InputStream is) {
                ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
                byte[] buffer = new byte[1024];
                int len;
                try {
                    while ((len = is.read(buffer)) >= 0) {
                        os.write(buffer, 0, len);
                    }
                } catch (java.io.IOException e) {
                }
                return os.toByteArray();
            }

            public SampleView(Context context) {
                super(context);



                    if(is==null)
                    {

                try {
                    is =context.getAssets().open("a.gif");
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                    }


                    mMovie = Movie.decodeStream(is);

            }

            public SampleView(Context context, AttributeSet attrs){
                super(context,attrs);

                    if(is==null)
                    {

                try {
                    is =context.getAssets().open("a.gif");
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                    }

                mMovie = Movie.decodeStream(is);
            }
            public SampleView(Context context, AttributeSet attrs, int defStyle){
                super(context,attrs,defStyle);

                    if(is==null)
                    {

                try {
                    is =context.getAssets().open("a.gif");
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                    }

                    mMovie = Movie.decodeStream(is);
            }


            @Override protected void onDraw(Canvas canvas) {

                long now = android.os.SystemClock.uptimeMillis();
                if (mMovieStart == 0) {   // first time
                    mMovieStart = now;
                }
                if (mMovie != null) {
                    int dur = mMovie.duration();
                    if (dur == 0) {
                        dur = 1000;
                    }
                    int relTime = (int)((now - mMovieStart) % dur);
                    mMovie.setTime(relTime);
                    mMovie.draw(canvas, getWidth() - mMovie.width(),
                                getHeight() - mMovie.height());
                    invalidate();
                }
            }
        } 

【问题讨论】:

  • 哦,拉达!也发布您的错误日志。
  • 为什么这些人都说“我遇到了异常”,但要么没有查看异常,要么在寻求帮助时没有发布任何详细信息?
  • 好的。 mMovie = Movie.decodeStream(is);这一行我得到空指针异常

标签: android nullpointerexception android-animation animated-gif


【解决方案1】:

试试这个,

public class GIFDemo extends GraphicsActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new GIFView(this));
}

private static class GIFView extends View {

    Movie movie;
    InputStream is = null;
    long moviestart;
    long moviestart1;

    public GIFView(Context context) {
        super(context);
        is = context.getResources()
                .openRawResource(R.drawable.animated_gif);

        movie = Movie.decodeStream(is);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(0xFFCCCCCC);
        super.onDraw(canvas);
        long now = android.os.SystemClock.uptimeMillis();
        System.out.println("now=" + now);
        if (moviestart == 0) { // first time
            moviestart = now;

        }

        System.out.println("\tmoviestart=" + moviestart);
        int relTime = (int) ((now - moviestart) % movie.duration());
        // int relTime1 = (int) ((now - moviestart1) % movie1.duration());
        System.out.println("time=" + relTime + "\treltime="
                + movie.duration());
        movie.setTime(relTime);
        // movie1.setTime(relTime1);
        movie.draw(canvas, 10, 10);
        // movie1.draw(canvas, 10, 100);
        this.invalidate();
    }
}
}

添加此 GraphicsActivity 类。

  class GraphicsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public void setContentView(View view) {

    super.setContentView(view);
}
}

【讨论】:

  • 对不起,我尝试使用你的代码。但现在我也得到了异常。我的日志结果是
  • java.lang.NullPointerException 02-27 11:52:09.737: E/AndroidRuntime(28198): at android.graphics.Movie.decodeStream(Native Method) 02-27 11:52:09.737: E/AndroidRuntime(28198): 在 com.example.kidsletters1.A$SampleView.(A.java:1081) 02-27 11:52:09.737: E/AndroidRuntime(28198): 在 com.example.kidsletters1 .A.onCreate(A.java:111)
  • @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);设置内容视图(R.layout.alh);样品视图=新样品视图(此); lp6 =新的RelativeLayout.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT); sampleview.setLayoutParams(lp6); lp6.setMargins(0,topsampl , 0, 0);
  • 我也想要xml文件所以我给了这样的,使用自定义类示例视图
  • 在同一个 GraphicsActivity 类中我还想显示一些 xml 内容
最近更新 更多