Android Studio 1.4.1中的运行时错误'IndexOutOfBoundsException'

时间:2015-10-30 00:48:29

标签: java android runtime indexoutofboundsexception android-bitmap

我正在尝试关注精灵动画上的youtube tutorial,并且一直遇到通常与activity相关的错误。这是一个:

  

java.lang.RuntimeException:无法启动活动ComponentInfo {rocketanimalcontrol.reap / rocketanimalcontrol.reap.MainActivity}:java.lang.IndexOutOfBoundsException:索引0无效,大小为0`

罪魁祸首是在哪里?

MainActivity.java

package rocketanimalcontrol.reap;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity; //? This needed with AppCompatActivity ?
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.view.View;
import android.view.Menu;
//import android.view.animation.Animation;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private ArrayList<Bitmap> bitmaps;//new here or in OnCreate?
    private SpriteAnimation animation;
    private CustomView view;

    //Auto-generated code
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
//    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = new CustomView(this);


        bitmaps = new ArrayList<Bitmap>();
        animation = new SpriteAnimation(bitmaps, 60);

        bitmaps.add(BitmapFactory.decodeResource(getResources(), R.drawable.sproutsmaller));
        bitmaps.add(BitmapFactory.decodeResource(getResources(), R.drawable.sproutsmaller2));
        bitmaps.add(BitmapFactory.decodeResource(getResources(), R.drawable.sproutsmaller3));
        bitmaps.add(BitmapFactory.decodeResource(getResources(), R.drawable.sproutsmaller2));
        bitmaps.add(BitmapFactory.decodeResource(getResources(), R.drawable.sproutsmaller3));
        bitmaps.add(BitmapFactory.decodeResource(getResources(), R.drawable.sproutsmaller4));

        animation = new SpriteAnimation(bitmaps, 60);
        view.setAnimation(animation);
        setContentView(view);

    }
//ERROR: cannot resolve menu symbol
//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        // Inflate the menu; this adds items to the action bar if it is present.
//        getMenuInflater().inflate(R.menu.main, menu);
//        return true;
//    }

}

CustomView.java

package rocketanimalcontrol.reap;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.SurfaceView;
import android.view.View;

public class CustomView extends View{

private SpriteAnimation animation;

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


}
@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawColor(Color.BLACK);
    canvas.drawBitmap(animation.getCurrentFrame(), 300, 200, new Paint());
    animation.update();
    //NOT thread safe
    try {
    Thread.sleep(animation.animationTime());
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    this.postInvalidate();
}


public void setAnimation(SpriteAnimation animation)
{
    this.animation = animation;
}

}

SpriteAnimation.java:

package rocketanimalcontrol.reap;

import java.util.ArrayList;
import android.graphics.Bitmap;

public class SpriteAnimation {

    private ArrayList<Bitmap> bitmaps;
    private Bitmap currentFrame;
    private int currentFrameIndex;
    private long frameTicker = (long) .01; //milliseconds
    private int animationTime;



    public SpriteAnimation (ArrayList<Bitmap> bitmaps, int fps)
    {
        this.bitmaps = bitmaps;
        currentFrame = bitmaps.get(0);
        currentFrameIndex = 0;
        animationTime = 1000 / fps; //milliseconds
    }

    public void update(long delta) {
        if (delta > frameTicker + animationTime) {
            // increment the frame
            currentFrameIndex++;
            if (currentFrameIndex >= bitmaps.size())
                currentFrameIndex = 0;
            currentFrame = bitmaps.get(currentFrameIndex);
        }
        // define the rectangle to cut out sprite

    }

    public int animationTime()
    {
        return this.animationTime;
    }

    public void update() {
        this.update(System.currentTimeMillis());
    }

    public Bitmap getCurrentFrame()
    {
        return currentFrame;
    }

    public ArrayList <Bitmap> getBitmaps()
    {
        return bitmaps;
    }

}

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="rocketanimalcontrol.reap" >
    android:versionCode="1"
    android:versionName="1.0">
    <application
        android:allowBackup="true"
        android:icon="@drawable/sproutsmaller4"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <!--"rocketanimalcontrol.reap.MainActivity"--> <!--.MainActivity-->
        <activity android:name="rocketanimalcontrol.reap.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>
    <uses-sdk android:minSdkVersion="4.4" />
</manifest>

完整的日志条目:(

11747-11747/? I/art: Not late-enabling -Xcheck:jni (already on)
11747-11747/? I/art: Late-enabling JIT
11747-11747/? I/art: JIT created with code_cache_capacity=2MB compile_threshold=1000
11747-11747/rocketanimalcontrol.reap W/System: ClassLoader referenced unknown path: /data/app/rocketanimalcontrol.reap-1/lib/x86
11747-11747/rocketanimalcontrol.reap D/AndroidRuntime: Shutting down VM
11747-11747/rocketanimalcontrol.reap E/AndroidRuntime: FATAL EXCEPTION: main
11747-11747/rocketanimalcontrol.reap E/AndroidRuntime: Process: rocketanimalcontrol.reap, PID: 11747
11747-11747/rocketanimalcontrol.reap E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{rocketanimalcontrol.reap/rocketanimalcontrol.reap.MainActivity}: java.lang.IndexOutOfBoundsException: Invalid index 
11747-11747/rocketanimalcontrol.reap E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
11747-11747/rocketanimalcontrol.reap E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
11747-11747/rocketanimalcontrol.reap E/AndroidRuntime:     at android.app.ActivityThread.-wrap11(ActivityThread.java)
11747-11747/rocketanimalcontrol.reap E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
11747-11747/rocketanimalcontrol.reap E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
11747-11747/rocketanimalcontrol.reap E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148)
11747-11747/rocketanimalcontrol.reap E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417)
11747-11747/rocketanimalcontrol.reap E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
11747-11747/rocketanimalcontrol.reap E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
11747-11747/rocketanimalcontrol.reap E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11747-11747/rocketanimalcontrol.reap E/AndroidRuntime:  Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
11747-11747/rocketanimalcontrol.reap E/AndroidRuntime:     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
11747-11747/rocketanimalcontrol.reap E/AndroidRuntime:     at java.util.ArrayList.get(ArrayList.java:308)
11747-11747/rocketanimalcontrol.reap E/AndroidRuntime:     at rocketanimalcontrol.reap.SpriteAnimation.<init>(SpriteAnimation.java:23)
11747-11747/rocketanimalcontrol.reap E/AndroidRuntime:     at rocketanimalcontrol.reap.MainActivity.onCreate(MainActivity.java:34)
11747-11747/rocketanimalcontrol.reap E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:6237)
11747-11747/rocketanimalcontrol.reap E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
11747-11747/rocketanimalcontrol.reap E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
11747-11747/rocketanimalcontrol.reap E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
11747-11747/rocketanimalcontrol.reap E/AndroidRuntime:     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
11747-11747/rocketanimalcontrol.reap E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
11747-11747/rocketanimalcontrol.reap E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
11747-11747/rocketanimalcontrol.reap E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148) 
11747-11747/rocketanimalcontrol.reap E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417) 
11747-11747/rocketanimalcontrol.reap E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
11747-11747/rocketanimalcontrol.reap E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
11747-11747/rocketanimalcontrol.reap E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
11747-11755/rocketanimalcontrol.reap W/art: Suspending all threads took: 5.267ms

感谢您的阅读,我已经在这三天的时间里待了几个小时而且非常沮丧。对不起,我不得不发布这么多代码,我真的不知道在哪里看,感到不堪重负。我尝试了java.lang.RuntimeException: Unable to instantiate activity ComponentInfoFunction in fragment makes emulator crash

1 个答案:

答案 0 :(得分:5)

在初始化位图列表之前,您正在调用SprintAnimation构造函数。初始化后再次调用它,看起来正确。删除第一个电话。

bitmaps = new ArrayList<Bitmap>();
animation = new SpriteAnimation(bitmaps, 60);  <--- REMOVE THIS LINE

bitmaps.add(BitmapFactory.decodeResource(getResources(), R.drawable.sproutsmaller));
bitmaps.add(BitmapFactory.decodeResource(getResources(),