java - Android IndexOutOfBound Exception : Invalid location 0, size is 0 -
i stuck , error when running gameview class. compiles no error when running on android emulation crashes. post logcat after code.
code
public class gameview extends view { private context mycontext; private list<card> deck = new arraylist<card>(); private int scaledcardw; private int scaledcardh; private int screenw; private int screenh; private list<card> myhand = new arraylist<card>(); private list<card> opphand = new arraylist<card>(); private list<card> discardpile = new arraylist<card>(); private float scale; private paint whitepaint; private int oppscore; private int myscore; public gameview(context context) { super(context); mycontext = context; scale = mycontext.getresources().getdisplaymetrics().density; whitepaint = new paint(); whitepaint.setantialias(true); whitepaint.setcolor(color.white); whitepaint.setstyle(paint.style.stroke); whitepaint.settextalign(paint.align.left); whitepaint.settextsize(scale * 15); } private void initcards() { (int = 0; < 4; i++) { (int j = 102; j < 115; j++) { int tempid = j + (i * 100); card tempcard = new card(tempid); int resourceid = getresources().getidentifier("card" + tempid, "drawable", mycontext.getpackagename()); bitmap tempbitmap = bitmapfactory.decoderesource( mycontext.getresources(), resourceid); scaledcardw = (int) (screenw / 8); scaledcardh = (int) (scaledcardw * 1.28); bitmap scaledbitmap = bitmap.createscaledbitmap(tempbitmap, scaledcardw, scaledcardh, false); tempcard.setbitmap(scaledbitmap); deck.add(tempcard); } } } private void drawcard(list<card> handtodraw) { handtodraw.add(0, deck.get(0)); deck.remove(0); if (deck.isempty()) { (int = discardpile.size() - 1; > 0; i--) { deck.add(discardpile.get(i)); discardpile.remove(i); collections.shuffle(deck, new random()); } } } private void dealcards() { collections.shuffle(deck, new random()); (int = 0; < 7; i++) { drawcard(myhand); drawcard(opphand); } } @override public void onsizechanged(int w, int h, int oldw, int oldh) { super.onsizechanged(w, h, oldw, oldh); dealcards(); } @override protected void ondraw(canvas canvas) { canvas.drawtext("computer score: " + integer.tostring(oppscore), 10, whitepaint.gettextsize() + 10, whitepaint); canvas.drawtext("my score: " + integer.tostring(myscore), 10, screenh - whitepaint.gettextsize() - 10, whitepaint); } }
logcat export.
03-18 07:51:46.795: w/dalvikvm(4395): threadid=3: thread exiting uncaught exception (group=0x4001b188) 03-18 07:51:46.795: e/androidruntime(4395): uncaught handler: thread main exiting due uncaught exception 03-18 07:51:46.805: e/androidruntime(4395): java.lang.indexoutofboundsexception: invalid location 0, size 0 03-18 07:51:46.805: e/androidruntime(4395): @ java.util.arraylist.get(arraylist.java:341) 03-18 07:51:46.805: e/androidruntime(4395): @ com.agpfd.crazyeights.gameview.drawcard(gameview.java:66) 03-18 07:51:46.805: e/androidruntime(4395): @ com.agpfd.crazyeights.gameview.dealcards(gameview.java:81) 03-18 07:51:46.805: e/androidruntime(4395): @ com.agpfd.crazyeights.gameview.onsizechanged(gameview.java:89) 03-18 07:51:46.805: e/androidruntime(4395): @ android.view.view.setframe(view.java:6897) 03-18 07:51:46.805: e/androidruntime(4395): @ android.view.view.layout(view.java:6824) 03-18 07:51:46.805: e/androidruntime(4395): @ android.widget.framelayout.onlayout(framelayout.java:333) 03-18 07:51:46.805: e/androidruntime(4395): @ android.view.view.layout(view.java:6830) 03-18 07:51:46.805: e/androidruntime(4395): @ android.widget.framelayout.onlayout(framelayout.java:333) 03-18 07:51:46.805: e/androidruntime(4395): @ android.view.view.layout(view.java:6830) 03-18 07:51:46.805: e/androidruntime(4395): @ android.view.viewroot.performtraversals(viewroot.java:996) 03-18 07:51:46.805: e/androidruntime(4395): @ android.view.viewroot.handlemessage(viewroot.java:1633) 03-18 07:51:46.805: e/androidruntime(4395): @ android.os.handler.dispatchmessage(handler.java:99) 03-18 07:51:46.805: e/androidruntime(4395): @ android.os.looper.loop(looper.java:123) 03-18 07:51:46.805: e/androidruntime(4395): @ android.app.activitythread.main(activitythread.java:4363) 03-18 07:51:46.805: e/androidruntime(4395): @ java.lang.reflect.method.invokenative(native method) 03-18 07:51:46.805: e/androidruntime(4395): @ java.lang.reflect.method.invoke(method.java:521) 03-18 07:51:46.805: e/androidruntime(4395): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:860) 03-18 07:51:46.805: e/androidruntime(4395): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:618) 03-18 07:51:46.805: e/androidruntime(4395): @ dalvik.system.nativestart.main(native method)
any appreciated.
initcards()
never called anywhere, deck list empty, calling handtodraw.add(0, deck.get(0))
in drawcard()
leads exception have, , indicates problem: you're trying access element @ index 0 of list of size 0:
invalid location 0, size 0
Comments
Post a Comment