你好朋友, 我想在Android中玩Gif,我试过这段代码:
package com.gifOperation;
import java.io.InputStream;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Movie;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
public class gifOperation extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new GIFView(this));
}
private static class GIFView extends View {
private static Movie movie;
InputStream is = null;
long moviestart;
public GIFView(Context context) {
super(context);
is = context.getResources().openRawResource(R.drawable.border_gif);
movie = Movie.decodeStream(is);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
long now = android.os.SystemClock.uptimeMillis();
if (moviestart == 0) {
moviestart = now;
}
int relTime = (int) ((now - moviestart) % movie.duration());
movie.setTime(relTime);
movie.draw(canvas, 200, 200);
this.invalidate();
}
}
}
但收到错误
07-26 10:32:55.073: ERROR/AndroidRuntime(626): FATAL EXCEPTION: main
07-26 10:32:55.073: ERROR/AndroidRuntime(626): java.lang.NullPointerException
07-26 10:32:55.073: ERROR/AndroidRuntime(626): at com.gifOperation.gifOperation$GIFView.onDraw(gifOperation.java:40)
07-26 10:32:55.073: ERROR/AndroidRuntime(626): at android.view.View.draw(View.java:6740)
07-26 10:32:55.073: ERROR/AndroidRuntime(626): at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
07-26 10:32:55.073: ERROR/AndroidRuntime(626): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
07-26 10:32:55.073: ERROR/AndroidRuntime(626): at android.view.View.draw(View.java:6743)
07-26 10:32:55.073: ERROR/AndroidRuntime(626): at android.widget.FrameLayout.draw(FrameLayout.java:352)
07-26 10:32:55.073: ERROR/AndroidRuntime(626): at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
07-26 10:32:55.073: ERROR/AndroidRuntime(626): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
07-26 10:32:55.073: ERROR/AndroidRuntime(626): at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
07-26 10:32:55.073: ERROR/AndroidRuntime(626): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
07-26 10:32:55.073: ERROR/AndroidRuntime(626): at android.view.View.draw(View.java:6743)
07-26 10:32:55.073: ERROR/AndroidRuntime(626): at android.widget.FrameLayout.draw(FrameLayout.java:352)
07-26 10:32:55.073: ERROR/AndroidRuntime(626): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1842)
07-26 10:32:55.073: ERROR/AndroidRuntime(626): at android.view.ViewRoot.draw(ViewRoot.java:1407)
07-26 10:32:55.073: ERROR/AndroidRuntime(626): at android.view.ViewRoot.performTraversals(ViewRoot.java:1163)
07-26 10:32:55.073: ERROR/AndroidRuntime(626): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
07-26 10:32:55.073: ERROR/AndroidRuntime(626): at android.os.Handler.dispatchMessage(Handler.java:99)
07-26 10:32:55.073: ERROR/AndroidRuntime(626): at android.os.Looper.loop(Looper.java:123)
07-26 10:32:55.073: ERROR/AndroidRuntime(626): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-26 10:32:55.073: ERROR/AndroidRuntime(626): at java.lang.reflect.Method.invokeNative(Native Method)
07-26 10:32:55.073: ERROR/AndroidRuntime(626): at java.lang.reflect.Method.invoke(Method.java:521)
07-26 10:32:55.073: ERROR/AndroidRuntime(626): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-26 10:32:55.073: ERROR/AndroidRuntime(626): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-26 10:32:55.073: ERROR/AndroidRuntime(626): at dalvik.system.NativeStart.main(Native Method)
有什么想法提出这个问题吗?
由于
帕特尔
答案 0 :(得分:0)
因为你必须定义另一个构造函数:像这样:
public GIFView(Context context, AttributeSet attrs){
super(context,attrs);
is = context.getResources().openRawResource(R.drawable.border_gif);
movie = Movie.decodeStream(is);
}
public GIFView(Context context, AttributeSet attrs, int defStyle){
super(context,attrs,defStyle);
is = context.getResources().openRawResource(R.drawable.border_gif);
movie = Movie.decodeStream(is);
}