Switch to surfaceview from another view

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Switch to surfaceview from another view

Postby mangaluve » Wed Mar 25, 2009 12:27 am

Im making a simple game. First I have a title screen, which is just a XML-view with some buttons. When I click one button, I want to change to my actual game, where I use a SurfaceView. But for some reason when I click the button to set the SurfaceView as contentview for my Activity, the window turns black. If I instead set my SurfaceView as the contentview immediatly in my Activity.onCreate, it works (it draws a yellow background). But it does not work when I start with my other view and switch to my SurfaceView. Here is the code

This is my title screen with just a button (main.xml)

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.    android:orientation="vertical"
  6.  
  7.    android:layout_width="fill_parent"
  8.  
  9.    android:layout_height="fill_parent"
  10.  
  11.    >
  12.  
  13. <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/start" android:text="start"></Button>
  14.  
  15. </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


This is the layout I use for the game, where com.switcher.TestSurface is a SurfaceView (surface.xml)
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3.  
  4.  
  5.     <com.switcher.TestSurface xmlns:android="http://schemas.android.com/apk/res/android"
  6.  
  7.      android:id="@+id/testsurface"
  8.  
  9.      android:layout_width="fill_parent"
  10.  
  11.      android:layout_height="fill_parent"/>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


This is my Activity
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.switcher;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.os.Bundle;
  9.  
  10. import android.view.View;
  11.  
  12. import android.view.View.OnClickListener;
  13.  
  14. import android.widget.Button;
  15.  
  16.  
  17.  
  18. public class SwitcherActivity extends Activity {
  19.  
  20.     public void onCreate(Bundle savedInstanceState) {
  21.  
  22.         super.onCreate(savedInstanceState);
  23.  
  24.         setContentView(R.layout.main);
  25.  
  26.        
  27.  
  28.         Button btnStart = (Button)findViewById(R.id.start);
  29.  
  30.         btnStart.setOnClickListener(new OnClickListener() {
  31.  
  32.                         public void onClick(View v) {
  33.  
  34.                                 setContentView(R.layout.surface);
  35.  
  36.                         }
  37.  
  38.                
  39.  
  40.         });
  41.  
  42.     }
  43.  
  44. }
  45.  
  46.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4

and this is my SurfaceView
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.switcher;
  3.  
  4.  
  5.  
  6. import android.content.Context;
  7.  
  8. import android.graphics.Canvas;
  9.  
  10. import android.util.AttributeSet;
  11.  
  12. import android.view.SurfaceHolder;
  13.  
  14. import android.view.SurfaceView;
  15.  
  16.  
  17.  
  18. public class TestSurface extends SurfaceView implements SurfaceHolder.Callback {
  19.  
  20.         private SurfaceHolder mSurfaceHolder;
  21.  
  22.         public TestSurface(Context context, AttributeSet attrs) {
  23.  
  24.                 super(context, attrs);
  25.  
  26.                
  27.  
  28.                 mSurfaceHolder = getHolder();
  29.  
  30.                 mSurfaceHolder.addCallback(this);
  31.  
  32.  
  33.  
  34.                 setFocusable(true);
  35.  
  36.         }
  37.  
  38.        
  39.  
  40.         private void doDraw(Canvas canvas) {
  41.  
  42.                 canvas.drawARGB(255, 255, 255, 0);
  43.  
  44.         }
  45.  
  46.        
  47.  
  48.         public void surfaceCreated(SurfaceHolder holder) {
  49.  
  50.                 Canvas c = null;
  51.  
  52.                 try {
  53.  
  54.                         c = mSurfaceHolder.lockCanvas(null);
  55.  
  56.                         synchronized (mSurfaceHolder) {
  57.  
  58.                                 doDraw(c);
  59.  
  60.                         }
  61.  
  62.                 } finally {
  63.  
  64.                         if (c != null) {
  65.  
  66.                                 mSurfaceHolder.unlockCanvasAndPost(c);
  67.  
  68.                         }
  69.  
  70.                 }
  71.  
  72.         }
  73.  
  74.        
  75.  
  76.         public void surfaceDestroyed(SurfaceHolder holder) {}
  77.  
  78.        
  79.  
  80.         public void surfaceChanged(SurfaceHolder holder, int format, int width,
  81.  
  82.                         int height) {}
  83.  
  84. }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


What is the problem?
mangaluve
Experienced Developer
Experienced Developer
 
Posts: 82
Joined: Mon Mar 23, 2009 8:59 pm

Top

Re: Switch to surfaceview from another view

Postby sarangkulkarni17 » Wed Mar 25, 2009 6:23 am

just refer to attached code. :)
Attachments
GameTemplate.zip
(47.37 KiB) Downloaded 168 times
sarangkulkarni17
Developer
Developer
 
Posts: 29
Joined: Fri Feb 13, 2009 10:00 am
Location: Pune

Postby mangaluve » Wed Mar 25, 2009 9:26 am

I'll keep that in mind in my next post :) Any idea why it's not working?
mangaluve
Experienced Developer
Experienced Developer
 
Posts: 82
Joined: Mon Mar 23, 2009 8:59 pm

Postby wiederke » Wed Mar 25, 2009 1:31 pm

Hi,

don't try to change the layout of your Activity. Instead use a second Activity and call it by creating an Intend. The second Activity uses your SurfaceView immediately and everything runs fine.


Cheers Torsten
wiederke
Experienced Developer
Experienced Developer
 
Posts: 72
Joined: Mon Mar 02, 2009 6:46 pm
Location: Germany

Postby mangaluve » Wed Mar 25, 2009 1:58 pm

So I cant change the View once I set it in an Activity? Or is this something special with SurfaceView.. because if I change it to another View, it works. What are the rules for changing the view?
mangaluve
Experienced Developer
Experienced Developer
 
Posts: 82
Joined: Mon Mar 23, 2009 8:59 pm

Postby wiederke » Wed Mar 25, 2009 11:03 pm

Hi,

the tutorials I used sofar, the view of an Activity was never changed. Don't know if this is best practice to change the Activity, if you change the whole layout of your view.


Cheers,

Torsten
wiederke
Experienced Developer
Experienced Developer
 
Posts: 72
Joined: Mon Mar 02, 2009 6:46 pm
Location: Germany

Top

Postby mangaluve » Thu Mar 26, 2009 10:03 am

Well I changed the XML for the SurfaceView so it's embedded in a LinearLayout (like the title screen) but it still does not work. I guess switching Activities is fine, but it would be nice to know why my approach doesnt work.
mangaluve
Experienced Developer
Experienced Developer
 
Posts: 82
Joined: Mon Mar 23, 2009 8:59 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 3 guests