将数据从 Activity 传递到 SurfaceView

发布于 2024-11-14 07:29:11 字数 2270 浏览 0 评论 0原文

我不确定问题出在哪里。我想活动是否会将信息传递给它实现的表面视图。基本上,我试图做到这一点,以便当有人从主菜单中选择他们选择的游戏布局(通过 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):致命异常:主要 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.InitationTargetException 06-06 17:55:01.655: ERROR/AndroidRuntime(16229): Caused by: java.lang.NullPointerException

然后一大堆堆大小溢出

所有这一切仅在我有 if(gameType == 1) 和 else if 时才会发生(gameType == 2) 在 loadImages 部分。这样做是为了在选择该地图时仅使用某些图像。不知道为什么这不起作用。

I'm not sure where the problem is. I thought if the activity would pass information to the surfaceview it implements. Basically I'm trying to make it so that when someone selects their choice of game layout from the main menu (passes it with Intent) it then goes to the PlayGame class. (I have it take the number given from the intent). That activity uses SurfaceView through a layout. Then down in the surface view where it calls the images and assigns them I tried using an if else to determine which pictures it should be using. Didn't work. Not exactly sure what to do about it. If there is a better way to make it use the images from a previous selection on the main page. Looks like this.

Main Class (Just one of the buttons passing in 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);
        }
    });

PlayGame Class

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


}

Game Class:

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

    }

More images than just one, but to save space it's just the one. Any help would be appreciated. I'm sure I must have done something wrong there.

EDIT: Here is the log cat from the force close caused from this setup.

06-06 17:55:01.655: ERROR/AndroidRuntime(16229): FATAL EXCEPTION: main
06-06 17:55:01.655: ERROR/AndroidRuntime(16229): java.lang.RuntimeException: Unable to start activity ComponentInfo{fallacystudios.gnarlyned/fallacystudios.gnarlyned.PlayGame}: android.view.InflateException: Binary XML file line #7: Error inflating class fallacystudios.gnarlyned.GameLoopView
06-06 17:55:01.655: ERROR/AndroidRuntime(16229): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-06 17:55:01.655: ERROR/AndroidRuntime(16229): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fallacystudios.gnarlyned.GameLoopView
06-06 17:55:01.655: ERROR/AndroidRuntime(16229): Caused by: java.lang.reflect.InvocationTargetException
06-06 17:55:01.655: ERROR/AndroidRuntime(16229): Caused by: java.lang.NullPointerException

Then a whole bunch of heap size overflow

All this only happens when I have the if(gameType == 1) and else if(gameType == 2) in the loadImages section. Doing this to make it so only certain images are used when that map is selected. Not sure why that doesnt work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一念一轮回 2024-11-21 07:29:11

如果从 Game 构造函数调用 loadImages ,那么问题是当您膨胀视图时,您尚未定义 playGame.type。尝试在调用 setContentView 之前提取额外的意图。

If loadImages is being called from the Game constructor, then the problem is that when you inflate the view, you haven't yet defined playGame.type. Try extracting the intent extra before calling setContentView.

呢古 2024-11-21 07:29:11

我已经做了你正在尝试的事情。我的解决方案是在内部类中设置所有视图,然后动态进行布局。这样每个小部件/视图都可以看到对方在做什么。

I've kind of done what you're attempting. My solution was to set all the views in inner classes and then do your layout dynamically. That way every widget/view can see what the other is doing.

怀中猫帐中妖 2024-11-21 07:29:11

PlayGame 类中的 type 设置为 public static ,并在调用 setContentView 之前调用 Intent extra,如 Ted Hopp 提到的。

set type to public static in PlayGame Class and call intent extra before calling setContentView as Ted Hopp mentioned.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文