GIFDecoder在运行时显示错误

时间:2014-02-12 11:51:54

标签: java android gif classnotfoundexception

我尝试使用GIF Decoder class为GIF文件制作动画。我按照this教程。现在代码没有显示任何错误,但在运行时它崩溃说java.lang.RuntimeException异常。 这是我的代码:

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(); 
}
}

第三节课是 GifDecoder.java.

我们是否也必须在Manifest中提及这些类。我什么也没做。

这是 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>

这是错误日志

 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

1 个答案:

答案 0 :(得分:0)

您在MainActivity.java

的第15行收到NullPointerException
at com.example.giftester.MainActivity.<init>(MainActivity.java:15)

请检查此错误。