Error my first gif image app on android studio

248 views Asked by At

I get error this (in logcat), and my emulator shows: Unfortunately, this app has stopped. I just want to animated GIFs in Android.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nasimjon.nasimjon/com.example.nasimjon.nasimjon.MainActivity}: java.lang.NullPointerException

This my Code:

public class GifView  extends View {

private Movie mMovie;
InputStream mStream;
long mMoviestart;


public GifView(Context context, InputStream stream) {
    super(context);
    mStream = stream;
    mMovie = Movie.decodeStream(mStream);
}

@Override
protected void onDraw(Canvas canvas){
    canvas.drawColor(Color.TRANSPARENT);
    super.onDraw(canvas);
    final long now = SystemClock.uptimeMillis();

    if (mMoviestart == 0){
        mMoviestart = now;
    }
    final int relTime = (int) ((now - mMoviestart)% mMovie.duration());
    mMovie.setTime(relTime);
    mMovie.draw(canvas, 10, 10);
    this.invalidate();
}

}

1

There are 1 answers

1
Bek-777 On

public class MainActivity extends Activity {

private WebView webViewActionView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   // this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    InputStream stream = null;

try{
        stream = getAssets().open("backtest.gif");

}catch (Exception e){
        e.printStackTrace();
    }

    webViewActionView = (WebView)findViewById(R.id.webViewActionView);
    webViewActionView.setWebViewClient(new MyWebViewClient());
    webViewActionView.getSettings().setJavaScriptEnabled(true);

    GifView view = new GifView(this, stream);
    webViewActionView.addView(view);

}

private class MyWebViewClient extends WebViewClient{ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }