gpt4 book ai didi

java - 将数据从 Activity 传递到 SurfaceView

转载 作者:行者123 更新时间:2023-11-30 11:53:29 25 4
gpt4 key购买 nike

我不确定问题出在哪里。我想如果这个 Activity 会把信息传递给它实现的表面 View 。基本上,我试图做到这一点,以便当有人从主菜单中选择他们选择的游戏布局(通过 Intent 传递)时,它会转到 PlayGame 类。 (我让它从 Intent 中获取数字)。该 Activity 通过布局使用 SurfaceView。然后在它调用图像并分配它们的表面 View 中,我尝试使用 if else 来确定它应该使用哪些图片。没用。不确定该怎么做。如果有更好的方法可以使用主页上先前选择的图像。看起来像这样。

主类(只是传递 Intent 的按钮之一)

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

游戏类

 @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): 致命异常: main06-06 17:55:01.655: ERROR/AndroidRuntime(16229): java.lang.RuntimeException: 无法启动 Activity ComponentInfo{fallacystudios.gnarlyned/fallacystudios.gnarlyned.PlayGame}: android.view.InflateException: 二进制 XML 文件行 # 7:错误膨胀类 fallacystudios.gnarlyned.GameLoopView06-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.GameLoopView06-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) 时,所有这一切才会发生。这样做是为了在选择该 map 时仅使用某些图像。不确定为什么那不起作用。

最佳答案

如果 loadImages 是从 Game 构造函数调用的,那么问题是当您膨胀 View 时,您还没有定义 playGame.type。在调用 setContentView 之前尝试提取额外的 Intent 。

关于java - 将数据从 Activity 传递到 SurfaceView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6240733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com