简体   繁体   English

将数据从 Activity 传递到 SurfaceView

[英]Pass data from the Activity to SurfaceView

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.基本上我试图做到这一点,以便当有人从主菜单中选择他们选择的游戏布局(通过 Intent 传递它)然后转到 PlayGame class。 (I have it take the number given from the intent). (我有它从意图中给出的数字)。 That activity uses SurfaceView through a layout.该活动通过布局使用 SurfaceView。 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.然后在它调用图像并分配它们的表面视图中,我尝试使用 if else 来确定它应该使用哪些图片。 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)主要 Class (只是传递意图的按钮之一)

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 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:游戏 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: Z93F725A0742 06-06 17:55:01.655:错误/AndroidRuntime(16229):致命异常:主要 06-06 17:55:01.655:错误/AndroidRuntime(16229):java.lang.RuntimeException:无法启动活动 ComponentInfo.{ 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:错误/AndroidRuntime(16229):原因:Z93F725A0742 3FE1C889F448B33D21F46Z.lang.reflect.InvocationTargetException 06-06 17:55:01.655: ERROR/AndroidRuntime(16229): Caused by: java.lang.NullPointerException 3FE1C889F448B33D21F46Z.lang.reflect.InvocationTargetException 06-06 17:55:01.655:错误/AndroidRuntime(16229):原因: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.只有当我在 loadImages 部分中有 if(gameType == 1) 和 else if(gameType == 2) 时,才会发生这一切。 Doing this to make it so only certain images are used when that map is selected.这样做是为了在选择 map 时仅使用某些图像。 Not sure why that doesnt work.不知道为什么这不起作用。

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.如果从 Game 构造函数调用loadImages ,那么问题是当你膨胀视图时,你还没有定义 playGame.type。 Try extracting the intent extra before calling setContentView .在调用setContentView之前尝试额外提取意图。

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.这样每个小部件/视图都可以看到另一个在做什么。

set type to public static in PlayGame Class and call intent extra before calling setContentView as Ted Hopp mentioned.PlayGame Class 中将type设置为public static并在调用setContentView之前额外调用意图,如 Ted Hopp 提到的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM