【发布时间】:2011-06-05 02:57:00
【问题描述】:
我不确定问题出在哪里。我想如果活动会将信息传递给它实现的表面视图。基本上我试图做到这一点,以便当有人从主菜单中选择他们选择的游戏布局(通过 Intent 传递它)然后转到 PlayGame 类。 (我有它从意图中给出的数字)。该活动通过布局使用 SurfaceView。然后在它调用图像并分配它们的表面视图中,我尝试使用 if else 来确定它应该使用哪些图片。没用。不完全确定该怎么做。如果有更好的方法可以使用主页上先前选择的图像。看起来像这样。
主类(只是传递意图的按钮之一)
Button surfButton = (Button) findViewById(R.id.play1_btn);
surfButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View surfing)
{
Intent play = new Intent(Main.this, PlayGame.class);
play.putExtra("gameType" , 1);
startActivity(play);
}
});
PlayGame 类
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent typ = getIntent();
type = typ.getIntExtra("gameType", 0);
setContentView(R.layout.game);
gameLoopView = (GameLoopView) findViewById(R.id.gnarlyned);
}
游戏类:
private void loadImages(Context context) {
type = playGame.type;
Resources res = context.getResources();
if (type == 1){
skyImage = BitmapFactory.decodeResource(res, R.drawable.sky);
}
else if (type == 2){
skyImage = BitmapFactory.decodeResource(res, R.drawable.snowbg);
}
更多图片不仅仅是一张,但为了节省空间,它只是一张。任何帮助,将不胜感激。我确定我在那里做错了什么。
编辑:这是此设置导致的强制关闭的日志猫。
06-06 17:55:01.655: 错误/AndroidRuntime(16229): 致命异常: main 06-06 17:55:01.655: 错误/AndroidRuntime(16229): java.lang.RuntimeException: 无法启动活动 ComponentInfo{fallacystudios.gnarlyned/fallacystudios.gnarlyned.PlayGame}: android.view.InflateException: 二进制 XML 文件行# 7: 膨胀类 fallacystudios.gnarlyned.GameLoopView 时出错 06-06 17:55:01.655: 错误/AndroidRuntime(16229): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 06-06 17:55:01.655:错误/AndroidRuntime(16229):原因:android.view.InflateException:二进制 XML 文件第 7 行:膨胀类 fallacystudios.gnarlyned.GameLoopView 时出错 06-06 17:55:01.655: 错误/AndroidRuntime(16229): 由: java.lang.reflect.InvocationTargetException 引起 06-06 17:55:01.655: 错误/AndroidRuntime(16229): 原因: java.lang.NullPointerException
然后一大堆堆大小溢出
只有当我在 loadImages 部分中有 if(gameType == 1) 和 else if(gameType == 2) 时,才会发生所有这些情况。这样做是为了在选择该地图时仅使用某些图像。不知道为什么这不起作用。
【问题讨论】:
-
什么实际上不起作用?我想我不明白确切的问题是什么。
-
究竟什么时候调用 loadImage(Context context) 方法?游戏类构造函数?来自 PlayGame 的 gameLoopView 对象? playGame.type 是什么类型,int 还是 Integer?
-
@Joe loadImage 在表面视图的公共游戏(上下文上下文)中被调用。从 PlayGame 中的 onCreate 调用表面视图。 playGame 类型是 int。
标签: java android surfaceview