GIFDe codeR显示运行时错误 [英] GIFDecoder showing error while running

查看:144
本文介绍了GIFDe codeR显示运行时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用 GIF德codeR类动画GIF文件。我跟着<一个href=\"http://droid-blog.net/2011/10/15/tutorial-how-to-play-animated-gifs-in-android-%E2%80%93-part-2/\"相对=nofollow>这个教程。现在,code未显示任何错误,但在运行时的崩溃说了java.lang.RuntimeException异常。
这里是我的code:

MainActivity.java

 公共类MainActivity扩展活动
{
    InputStream的流= NULL;
    {
    尝试
    {
        流= getAssets()开(piggy.gif);
    }
    赶上(IOException异常E)
    {
        e.printStackTrace();
    }
}
}

GIFDe coderView.java

 公共类GifDe coderView延伸的ImageView
{
    公共GifDe coderView(上下文的背景下,InputStream的流)
    {
        超级(上下文);
        playGif(流);
    }    私人布尔mIsPlayingGif = FALSE;
    私人GifDe codeR mGifDe codeR;
    私人位图mTmpBitmap;
    最后的处理程序mHandler =新的处理程序();
    最终的Runnable mUpdateResults =新的Runnable()
    {
        公共无效的run()
        {
            如果(mTmpBitmap = NULL&放大器;!&安培;!mTmpBitmap.isRecycled())
            {
                GifDe coderView.this.setImageBitmap(mTmpBitmap);
                }
            }
        };        私人无效playGif(InputStream的流)
        {
            mGifDe codeR =新GifDe codeR();
            mGifDe coder.read(流);
            mIsPlayingGif = TRUE;
        新主题(新的Runnable()
        {
            公共无效的run()
        {
                最终诠释N = mGifDe coder.getFrameCount();
                最终诠释ntimes = mGifDe coder.getLoopCount();
                INT repetitionCounter = 0; {做对(INT I = 0; I&LT; N;我++)
                {
                    mTmpBitmap = mGifDe coder.getFrame(I)
                    最终诠释T = mGifDe coder.getDelay(I)
                    mHandler.post(mUpdateResults);
                    尝试
                    {
                        视频下载(T);
                        }
                    赶上(InterruptedException的E)
                    {
                        e.printStackTrace();
                        }
                    }
                如果(ntimes!= 0)
                {repetitionCounter ++;
                }
                }
                而(mIsPlayingGif&放大器;及(repetitionCounter&下; = ntimes));
                    }
            })。开始();
}
}

和第三类的 GifDe coder.java。

我们是否也提到在清单这些类?我什么也没有做。

下面就是的Manifest.xml

 &LT;?XML版本=1.0编码=UTF-8&GT?;
&LT;清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.example.giftester
    安卓版code =1
    机器人:=的versionName1.0&GT;    &LT;用途-SDK
        安卓的minSdkVersion =8
        机器人:targetSdkVersion =18/&GT;    &lt;应用
        机器人:allowBackup =真
        机器人:图标=@绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@风格/ AppTheme&GT;
        &LT;活动
            机器人:名字=com.example.giftester.MainActivity
            机器人:标签=@字符串/ APP_NAME&GT;
            &所述;意图滤光器&gt;
                &lt;作用机器人:名字=android.intent.action.MAIN/&GT;                &LT;类机器人:名字=android.intent.category.LAUNCHER/&GT;
            &所述; /意图滤光器&gt;
        &LT; /活性GT;
    &LT; /用途&gt;&LT; /清单&GT;

这是在错误日志

  02-12 17:30:29.266:E / AndroidRuntime(1530年):致命异常:主要
02-12 17:30:29.266:E / AndroidRuntime(1530年):了java.lang.RuntimeException:无法实例活动ComponentInfo {com.example.giftester / com.example.giftester.MainActivity}:显示java.lang.NullPointerException
02-12 17:30:29.266:E / AndroidRuntime(1530年):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
02-12 17:30:29.266:E / AndroidRuntime(1530年):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
02-12 17:30:29.266:E / AndroidRuntime(1530年):在android.app.ActivityThread.access $ 600(ActivityThread.java:130)
02-12 17:30:29.266:E / AndroidRuntime(1530年):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1195)
02-12 17:30:29.266:E / AndroidRuntime(1530年):在android.os.Handler.dispatchMessage(Handler.java:99)
02-12 17:30:29.266:E / AndroidRuntime(1530年):在android.os.Looper.loop(Looper.java:137)
02-12 17:30:29.266:E / AndroidRuntime(1530年):在android.app.ActivityThread.main(ActivityThread.java:4745)
02-12 17:30:29.266:E / AndroidRuntime(1530年):在java.lang.reflect.Method.invokeNative(本机方法)
02-12 17:30:29.266:E / AndroidRuntime(1530年):在java.lang.reflect.Method.invoke(Method.java:511)
02-12 17:30:29.266:E / AndroidRuntime(1530年):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786)
02-12 17:30:29.266:E / AndroidRuntime(1530年):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-12 17:30:29.266:E / AndroidRuntime(1530年):在dalvik.system.NativeStart.main(本机方法)
02-12 17:30:29.266:E / AndroidRuntime(1530):因:显示java.lang.NullPointerException
02-12 17:30:29.266:E / AndroidRuntime(1530年):在android.content.ContextWrapper.getAssets(ContextWrapper.java:75)
02-12 17:30:29.266:E / AndroidRuntime(1530年):在com.example.giftester.MainActivity&LT;&初始化GT;(MainActivity.java:15)
02-12 17:30:29.266:E / AndroidRuntime(1530年):在java.lang.Class.newInstanceImpl(本机方法)
02-12 17:30:29.266:E / AndroidRuntime(1530年):在java.lang.Class.newInstance(Class.java:1319)
02-12 17:30:29.266:E / AndroidRuntime(1530年):在android.app.Instrumentation.newActivity(Instrumentation.java:1053)
02-12 17:30:29.266:E / AndroidRuntime(1530年):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
02-12 17:30:29.266:E / AndroidRuntime(1530):11 ...更多


解决方案

您是在 MainActivity.java

第15行得到一个NullPointerException异常

 在com.example.giftester.MainActivity&LT;&初始化GT;(MainActivity.java:15)

请检查这个错误。

I tried to use the GIF Decoder class to animate GIF file. I followed this tutorial. Now the code is not showing any error but at runtime its crashing saying java.lang.RuntimeException exception. Here is my code:

MainActivity.java

public class MainActivity extends Activity
{
    InputStream stream = null; 
    {
    try 
    { 
        stream = getAssets().open("piggy.gif");
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}
}

GIFDecoderView.java

public class GifDecoderView extends ImageView 
{


    public GifDecoderView(Context context, InputStream stream) 
    {    
        super(context);       
        playGif(stream);
    }

    private boolean mIsPlayingGif = false;    
    private GifDecoder mGifDecoder;    
    private Bitmap mTmpBitmap;   
    final Handler mHandler = new Handler(); 
    final Runnable mUpdateResults = new Runnable() 
    {
        public void run() 
        { 
            if (mTmpBitmap != null && !mTmpBitmap.isRecycled()) 
            { 
                GifDecoderView.this.setImageBitmap(mTmpBitmap); 
                } 
            } 
        };

        private void playGif(InputStream stream) 
        { 
            mGifDecoder = new GifDecoder();
            mGifDecoder.read(stream);
            mIsPlayingGif = true; 


        new Thread(new Runnable() 
        { 
            public void run() 
        { 
                final int n = mGifDecoder.getFrameCount(); 
                final int ntimes = mGifDecoder.getLoopCount();
                int repetitionCounter = 0; do { for (int i = 0; i < n; i++)
                { 
                    mTmpBitmap = mGifDecoder.getFrame(i); 
                    final int t = mGifDecoder.getDelay(i);
                    mHandler.post(mUpdateResults); 
                    try 
                    { 
                        Thread.sleep(t); 
                        } 
                    catch (InterruptedException e) 
                    {
                        e.printStackTrace();
                        } 
                    } 
                if(ntimes != 0) 
                { repetitionCounter ++; 
                } 
                } 
                while (mIsPlayingGif && (repetitionCounter <= ntimes));
                    }     
            }).start(); 
}
}

And the 3rd class is GifDecoder.java.

Do we have to mention these classes in Manifest also.? I have done nothing.

Here is Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.giftester"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.giftester.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

And here is the Error Log

 02-12 17:30:29.266: E/AndroidRuntime(1530): FATAL EXCEPTION: main
02-12 17:30:29.266: E/AndroidRuntime(1530): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.giftester/com.example.giftester.MainActivity}: java.lang.NullPointerException
02-12 17:30:29.266: E/AndroidRuntime(1530):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
02-12 17:30:29.266: E/AndroidRuntime(1530):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
02-12 17:30:29.266: E/AndroidRuntime(1530):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
02-12 17:30:29.266: E/AndroidRuntime(1530):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
02-12 17:30:29.266: E/AndroidRuntime(1530):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-12 17:30:29.266: E/AndroidRuntime(1530):     at android.os.Looper.loop(Looper.java:137)
02-12 17:30:29.266: E/AndroidRuntime(1530):     at android.app.ActivityThread.main(ActivityThread.java:4745)
02-12 17:30:29.266: E/AndroidRuntime(1530):     at java.lang.reflect.Method.invokeNative(Native Method)
02-12 17:30:29.266: E/AndroidRuntime(1530):     at java.lang.reflect.Method.invoke(Method.java:511)
02-12 17:30:29.266: E/AndroidRuntime(1530):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-12 17:30:29.266: E/AndroidRuntime(1530):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-12 17:30:29.266: E/AndroidRuntime(1530):     at dalvik.system.NativeStart.main(Native Method)
02-12 17:30:29.266: E/AndroidRuntime(1530): Caused by: java.lang.NullPointerException
02-12 17:30:29.266: E/AndroidRuntime(1530):     at android.content.ContextWrapper.getAssets(ContextWrapper.java:75)
02-12 17:30:29.266: E/AndroidRuntime(1530):     at com.example.giftester.MainActivity.<init>(MainActivity.java:15)
02-12 17:30:29.266: E/AndroidRuntime(1530):     at java.lang.Class.newInstanceImpl(Native Method)
02-12 17:30:29.266: E/AndroidRuntime(1530):     at java.lang.Class.newInstance(Class.java:1319)
02-12 17:30:29.266: E/AndroidRuntime(1530):     at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
02-12 17:30:29.266: E/AndroidRuntime(1530):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
02-12 17:30:29.266: E/AndroidRuntime(1530):     ... 11 more

解决方案

You are getting a NullPointerException at line 15 of MainActivity.java

at com.example.giftester.MainActivity.<init>(MainActivity.java:15)

Please check for this error.

这篇关于GIFDe codeR显示运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆