当我加载我的一个活动时,我正在接收android.view.inflateException。它似乎没有提前做到这一点,但是因为我将启动活动更改为另一个启动活动,现在启动一个不按按钮的工作。它说链表上有一个indexOutOfBoundsException但我没有看到任何改变(除了drawable文件夹的内容)。谢谢你的帮助!
这是Logcat:
04-05 11:03:31.566: E/AndroidRuntime(6112): FATAL EXCEPTION: main
04-05 11:03:31.566: E/AndroidRuntime(6112): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.semantic.games/net.semantic.games.AndrongActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class net.semantic.games.AndrongSurfaceView
04-05 11:03:31.566: E/AndroidRuntime(6112): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
04-05 11:03:31.566: E/AndroidRuntime(6112): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
04-05 11:03:31.566: E/AndroidRuntime(6112): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-05 11:03:31.566: E/AndroidRuntime(6112): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
04-05 11:03:31.566: E/AndroidRuntime(6112): at android.os.Handler.dispatchMessage(Handler.java:99)
04-05 11:03:31.566: E/AndroidRuntime(6112): at android.os.Looper.loop(Looper.java:130)
04-05 11:03:31.566: E/AndroidRuntime(6112): at android.app.ActivityThread.main(ActivityThread.java:3687)
04-05 11:03:31.566: E/AndroidRuntime(6112): at java.lang.reflect.Method.invokeNative(Native Method)
04-05 11:03:31.566: E/AndroidRuntime(6112): at java.lang.reflect.Method.invoke(Method.java:507)
04-05 11:03:31.566: E/AndroidRuntime(6112): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
04-05 11:03:31.566: E/AndroidRuntime(6112): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
04-05 11:03:31.566: E/AndroidRuntime(6112): at dalvik.system.NativeStart.main(Native Method)
04-05 11:03:31.566: E/AndroidRuntime(6112): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class net.semantic.games.AndrongSurfaceView
04-05 11:03:31.566: E/AndroidRuntime(6112): at android.view.LayoutInflater.createView(LayoutInflater.java:518)
04-05 11:03:31.566: E/AndroidRuntime(6112): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
04-05 11:03:31.566: E/AndroidRuntime(6112): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
04-05 11:03:31.566: E/AndroidRuntime(6112): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
04-05 11:03:31.566: E/AndroidRuntime(6112): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
04-05 11:03:31.566: E/AndroidRuntime(6112): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
04-05 11:03:31.566: E/AndroidRuntime(6112): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:214)
04-05 11:03:31.566: E/AndroidRuntime(6112): at android.app.Activity.setContentView(Activity.java:1657)
04-05 11:03:31.566: E/AndroidRuntime(6112): at net.semantic.games.AndrongActivity.onCreate(AndrongActivity.java:24)
04-05 11:03:31.566: E/AndroidRuntime(6112): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-05 11:03:31.566: E/AndroidRuntime(6112): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
04-05 11:03:31.566: E/AndroidRuntime(6112): ... 11 more
04-05 11:03:31.566: E/AndroidRuntime(6112): Caused by: java.lang.reflect.InvocationTargetException
04-05 11:03:31.566: E/AndroidRuntime(6112): at java.lang.reflect.Constructor.constructNative(Native Method)
04-05 11:03:31.566: E/AndroidRuntime(6112): at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
04-05 11:03:31.566: E/AndroidRuntime(6112): at android.view.LayoutInflater.createView(LayoutInflater.java:505)
04-05 11:03:31.566: E/AndroidRuntime(6112): ... 21 more
04-05 11:03:31.566: E/AndroidRuntime(6112): Caused by: java.lang.IndexOutOfBoundsException
04-05 11:03:31.566: E/AndroidRuntime(6112): at java.util.LinkedList.get(LinkedList.java:520)
04-05 11:03:31.566: E/AndroidRuntime(6112): at net.semantic.games.Sprite.getWidth(Sprite.java:84)
04-05 11:03:31.566: E/AndroidRuntime(6112): at net.semantic.games.Sprite.centerHorizontal(Sprite.java:208)
04-05 11:03:31.566: E/AndroidRuntime(6112): at net.semantic.games.Sprite.center(Sprite.java:202)
04-05 11:03:31.566: E/AndroidRuntime(6112): at net.semantic.games.AndrongThread.<init>(AndrongThread.java:53)
04-05 11:03:31.566: E/AndroidRuntime(6112): at net.semantic.games.AndrongSurfaceView.CreateNewAndrongThread(AndrongSurfaceView.java:57)
04-05 11:03:31.566: E/AndroidRuntime(6112): at net.semantic.games.AndrongSurfaceView <init>(AndrongSurfaceView.java:28)
04-05 11:03:31.566: E/AndroidRuntime(6112): ... 24 more
04-05 11:03:31.578: E/(109): Dumpstate > /data/log/dumpstate_app_error
以下是活动的XML文件:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<net.semantic.games.AndrongSurfaceView
android:id="@+id/androng"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/text"
android:text="Androng"
android:visibility="visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10px"
android:gravity="center_horizontal"
android:textColor="#99FFFFFF"
android:textSize="16sp"/>
</RelativeLayout>
</FrameLayout>
Android Surface View类:
package net.semantic.games;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.TextView;
import android.widget.Toast;
public class AndrongSurfaceView extends SurfaceView implements SurfaceHolder.Callback
{
private AndrongThread androngThread;
private TextView statusText;
private SurfaceHolder holder;
private Context context;
public AndrongSurfaceView(Context context, AttributeSet attrs)
{
super(context, attrs);
this.context = context;
this.holder = getHolder();
holder.addCallback(this);
setFocusable(true);
androngThread = CreateNewAndrongThread();
}
public void setTextView(TextView textView)
{
statusText = textView;
}
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder)
{
androngThread.setRunning(true);
try
{
androngThread.start();
}
catch (Exception error)
{
androngThread = CreateNewAndrongThread();
androngThread.start();
androngThread.setRunning(true);
}
}
private AndrongThread CreateNewAndrongThread()
{
Toast toast = Toast.makeText(context, "Select Menu for a new game", Toast.LENGTH_LONG);
toast.show();
return new AndrongThread(holder, context, new Handler()
{
@Override
public void handleMessage(Message m)
{
Bundle bundle = m.getData();
if (bundle.containsKey("viz"))
{
statusText.setVisibility(m.getData().getInt("viz"));
statusText.setText(m.getData().getString("text"));
}
else if (bundle.containsKey("toast"))
{
Toast toast = Toast.makeText(context, bundle.getString("toast"), Toast.LENGTH_LONG);
toast.show();
}
}
});
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height)
{
androngThread.setSurfaceSize(width, height);
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder)
{
androngThread.setRunning(false);
boolean retry = true;
while (retry)
{
try
{
androngThread.join();
retry = false;
}
catch (InterruptedException e)
{
}
}
}
public AndrongThread getAndroidPongThread()
{
return androngThread;
}
@Override
public void onWindowFocusChanged(boolean hasWindowFocus)
{
if (!hasWindowFocus)
{
androngThread.pause();
}
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
float xPosition1 = 0;
float yPosition1 = 0;
float xPosition2 = 0;
float yPosition2 = 0;
for (int pointerIndex = 0; pointerIndex < event.getPointerCount(); pointerIndex++)
{
if (pointerIndex == 0)
{
xPosition1 = event.getX(pointerIndex);
yPosition1 = event.getY(pointerIndex);
}
if (pointerIndex == 1)
{
xPosition2 = event.getX(pointerIndex);
yPosition2 = event.getX(pointerIndex);
}
}
switch (event.getAction())
{
case MotionEvent.ACTION_MOVE:
androngThread.setBattPosition(xPosition1, yPosition1, xPosition2, yPosition2);
break;
}
return true;
}
}
DrawableResourceCollection类:
import android.graphics.drawable.Drawable;
import java.util.LinkedList;
public class DrawableResourceCollection extends LinkedList<Drawable>
{
}
精灵类: package net.semantic.games;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.os.SystemClock;
public class Sprite
{
protected double xPosition;
protected double yPosition;
protected DrawableResourceCollection drawableResourceCollection;
private Bitmap internalBitmap;
private int canvasWidth;
private int canvasHeight;
protected Velocity velocity;
private int currentFrame = 0;
private double animationSpeedInFpms;
private long startTimeInMillis;
private boolean animationForward;
public Sprite(DrawableResourceCollection drawableResourceCollection, int canvasWidth, int canvasHeight, int animationSpeedInFps, boolean animationForward)
{
this.canvasWidth = canvasWidth;
this.canvasHeight = canvasHeight;
this.drawableResourceCollection = drawableResourceCollection;
this.animationSpeedInFpms = animationSpeedInFps / 1000.0;
this.startTimeInMillis = SystemClock.uptimeMillis();
this.animationForward = animationForward;
}
public double getxPosition()
{
return xPosition;
}
public void draw(Canvas canvas)
{
drawableResourceCollection.get(currentFrame).setBounds((int) xPosition, (int) yPosition, (int) xPosition + getWidth(), (int) yPosition + getHeight());
drawableResourceCollection.get(currentFrame).draw(canvas);
currentFrame = 1;
}
public void canvasChanges(int canvasWidth, int canvasHeight)
{
this.canvasWidth = canvasWidth;
this.canvasHeight = canvasHeight;
}
public void setxPosition(int xPosition)
{
this.xPosition = xPosition;
}
public double getyPosition()
{
return yPosition;
}
public void setyPosition(int yPosition)
{
this.yPosition = yPosition;
}
public void reverseAnimation()
{
animationForward = !animationForward;
}
public int getWidth()
{
for(int i = 0; i < 10; i++){
}
return drawableResourceCollection.get(currentFrame).getIntrinsicWidth();
}
public int getHeight()
{
return drawableResourceCollection.get(currentFrame).getIntrinsicHeight();
}
public void Move(double frameTime)
{
double newXPosition = velocity.getNewXPosition(xPosition, frameTime);
if (newXPosition + getWidth() < (canvasWidth - 4) && newXPosition > 4)
xPosition = newXPosition;
yPosition = velocity.getNewYPosition(yPosition, frameTime);
}
public boolean CollidesWith(Sprite sprite, double frameTime)
{
int left1, left2;
int right1, right2;
int top1, top2;
int bottom1, bottom2;
left1 = (int) this.getNewXPosition(frameTime);
left2 = (int) sprite.getNewXPosition(frameTime);
right1 = left1 + this.getWidth();
right2 = left2 + sprite.getWidth();
top1 = (int) this.getNewYPosition(frameTime);
top2 = (int) sprite.getNewYPosition(frameTime);
bottom1 = top1 + this.getHeight();
bottom2 = top2 + sprite.getHeight();
if (bottom1 < top2)
return false;
if (top1 > bottom2)
return false;
if (right1 < left2)
return false;
if (left1 > right2)
return false;
int over_bottom;
int over_top;
int over_right;
int over_left;
if (bottom1 > bottom2)
over_bottom = bottom2;
else
over_bottom = bottom1;
if (top1 < top2)
over_top = top2;
else
over_top = top1;
if (right1 > right2)
over_right = right2;
else
over_right = right1;
if (left1 < left2)
over_left = left2;
else
over_left = left1;
for (int xPos = over_left; xPos < over_right; xPos++)
{
for (int yPos = over_top; yPos < over_bottom; yPos++)
{
int thisColor = this.getPixel(xPos, yPos);
int spriteColor = sprite.getPixel(xPos, yPos);
if (thisColor != 0 && spriteColor != 0)
{
return true;
}
}
}
return false;
}
public double getNewXPosition(double frameTime)
{
return velocity.getNewXPosition(xPosition, frameTime);
}
public double getNewYPosition(double frameTime)
{
return velocity.getNewYPosition(yPosition, frameTime);
}
private int getPixel(int x, int y)
{
int xPosInBitmap;
if (x > getxPosition())
xPosInBitmap = x - (int) getxPosition();
else
xPosInBitmap = (int) getxPosition() - x;
int yPosInBitmap;
if (y > getyPosition())
yPosInBitmap = y - (int) getyPosition();
else
yPosInBitmap = (int) getyPosition() - y;
if (x >= internalBitmap.getWidth() || y >= internalBitmap.getHeight())
return 1;
if (yPosInBitmap < this.internalBitmap.getHeight() && xPosInBitmap < internalBitmap.getWidth())
return this.internalBitmap.getPixel(xPosInBitmap, yPosInBitmap);
else
return 0;
}
public void center()
{
centerHorizontal();
centerVertical();
}
public void centerHorizontal()
{
setxPosition(canvasWidth / 2 - getWidth() / 2);
}
public void centerVertical()
{
setyPosition(canvasHeight / 2 - getHeight() / 2);
}
public boolean IsMaximumRight(int canvasWidth)
{
return getxPosition() >= (canvasWidth - getWidth());
}
public void SetMaximumRight(int canvasWidth)
{
setxPosition(canvasWidth - getWidth());
}
public void setVelocity(Velocity velocity)
{
this.velocity = velocity;
}
public void setPreviousLocation(double frameTime)
{
xPosition = velocity.getPreviousXPosition(xPosition, frameTime);
yPosition = velocity.getPreviousYPosition(yPosition, frameTime);
}
public boolean IsOutOfXBounds(double frameTime)
{
return (getNewXPosition(frameTime) <= 7) || (getNewXPosition(frameTime) + getWidth() >= canvasWidth - 7);
}
public boolean IsOutOfLowerBounds(double frameTime)
{
return ((int) getNewYPosition(frameTime) + getHeight() >= canvasHeight - 10);
}
public boolean IsOutOfUpperBounds(double frameTime)
{
return getNewYPosition(frameTime) <= 10;
}
}
答案 0 :(得分:0)
感谢Raghunandan的帮助,我明白了。原来它从未填充drawableResourceCollection因此它变为空。请在评论中投票给他答案