Surfaceview to new Activity Issue

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

Surfaceview to new Activity Issue

Postby kdiddy » Sun Oct 11, 2009 9:12 pm

Hi,

I have an activity which uses a surface view class to display the graphics of my app.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class MainClass extends Activity
  3.  
  4.  
  5.  
  6.          public void onCreate(Bundle icicle) {
  7.  
  8.               super.onCreate(icicle)
  9.  
  10.                 this.theViewClass = new theViewClass (this);
  11.  
  12.                 setContentView(this.theViewClass );  
  13.  
  14.          }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Everything is handled in the view class (which extends surfaceview) but eventually I want to load a new activity.

So in the view class a condition is eventually met and I want to start a new Activity. I'm unable to use the following in the view class

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Intent intent = new Intent(theViewClass.this, NewClass.class);
  2.  
  3.                 startActivity(intent);
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


So I tried creating an instance of my MainClass Activity and running a method I created in the main class to handle the intent. Unfortunately that didn't work either. There is a lot of great code on this site for graphics and surfaceviews, but how do you get out of the surface view and load a new activity?

Any ideas?

Thanks, any help is appreciated.

K
Diddy
kdiddy
Junior Developer
Junior Developer
 
Posts: 19
Joined: Wed Apr 01, 2009 2:30 am

Top

Postby Sowmya » Mon Oct 12, 2009 5:55 am

Hi,
Try like this
((Activity) context).startActivity(intent);

Activity is a subclass of Context
Sowmya
Experienced Developer
Experienced Developer
 
Posts: 51
Joined: Fri Mar 06, 2009 7:49 am

Postby kdiddy » Mon Oct 12, 2009 6:32 pm

Thanks Sowmya,

Just so I'm clear...are you saying that I can run the intent in my View class but have to refer to the main activity and context in order to run startActivity?

Could you please give me an example in code?

Is it something like

MainClass.context.startActivity(intent)

Thanks so much for your help!

K
Diddy
kdiddy
Junior Developer
Junior Developer
 
Posts: 19
Joined: Wed Apr 01, 2009 2:30 am

Postby Sowmya » Tue Oct 13, 2009 6:13 am

Hi Diddy,
Please find the example code...

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class Test extends Activity {    
  2.  
  3.     private Preview mPreview;
  4.  
  5.  
  6.  
  7.     @Override
  8.  
  9.         protected void onCreate(Bundle savedInstanceState) {
  10.  
  11.         super.onCreate(savedInstanceState);
  12.  
  13.         mPreview = new Preview(this);        
  14.  
  15.         setContentView(mPreview);
  16.  
  17. }
  18.  
  19. }
  20.  
  21.  
  22.  
  23.  
  24.  
  25. class Preview extends SurfaceView implements SurfaceHolder.Callback {
  26.  
  27.        
  28.  
  29.     Preview(Context context){
  30.  
  31.         super(context);
  32.  
  33.         //Intent code here
  34.  
  35.         ((Activity) context).startActivity(intent);
  36.  
  37.  
  38.  
  39. }
  40.  
  41. }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Hope this helps.
Sowmya
Experienced Developer
Experienced Developer
 
Posts: 51
Joined: Fri Mar 06, 2009 7:49 am

Postby kdiddy » Wed Oct 14, 2009 12:11 am

Hi Sowmya,

Thank you for your reply. I feel like I'm getting closer but to no avail. So I tried a few things....I put the code in as you had written but I'm still getting a force close. I also tried creating an Activity object then using that object for the intent.

These all seem like things that should work. One other piece is that my surface view handles the canvas drawings in a thread. I thought maybe I was throwing an error because I was suddenly interrupting the thread by starting a new activity, so I stop the thread prior to running the intent but am still having the same issue. I tried setting the running to false, also thread.stop();, and thread.destroy();(not all at once) but still having the same issue.

Thank you very much for your help!

K
Diddy
kdiddy
Junior Developer
Junior Developer
 
Posts: 19
Joined: Wed Apr 01, 2009 2:30 am

Postby Sowmya » Wed Oct 14, 2009 6:28 am

Hi Diddy,
What is the error you are getting in logcat?Can you post your code here?
One more question,did you register the new activity that you are calling in the Androidmanifest xml file

Regards,
Sowmya
Sowmya
Experienced Developer
Experienced Developer
 
Posts: 51
Joined: Fri Mar 06, 2009 7:49 am

Top

Postby kdiddy » Thu Oct 15, 2009 1:43 am

Hi,

I've included my code for the main class, the view class (which extends SurfaceView), and the thread. Regarding errors, I'm not a pro at the debugger yet but I was first getting an error regarding my CountDownTimer but now I'm getting a "source not found" in a new tab next to my classes which reads "ViewRoot.handleMessage(message) line: 1704, but that is not showing up the logcat. Not sure what that means.

The logcat just has a bunch of warnings regarding "executeScheduledBroadcast" Nothing red. Also the class I'm trying to load is properly entered into the manifest.

I left two ways in the code below regarding how I was trying to make this work in the app. First you'll see a condition is met within the timer "doneGame" changes to 1 which runs the the endGame1() method within the AppView class. Second, the ontouch event tries to trigger a method in the MainClass class.

So again, the goal here is to get the HighScore activity to load

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4. public class MainClass extends Activity {
  5.  
  6.        
  7.  
  8.         AppView theAppView = null;
  9.  
  10.        
  11.  
  12.     @Override
  13.  
  14.     public void onCreate(Bundle savedInstanceState) {
  15.  
  16.         super.onCreate(savedInstanceState);
  17.  
  18.        
  19.  
  20.          this.theAppView = new AppView(this);
  21.  
  22.          setContentView(this.theAppView);    
  23.  
  24.        
  25.  
  26.     }
  27.  
  28.    
  29.  
  30.     public void endGame1() {
  31.  
  32.         Intent intent = new Intent(MainClass.this, HighScoreScreen.class);
  33.  
  34.         startActivity(intent); 
  35.  
  36.         finish();
  37.  
  38.     }
  39.  
  40.    
  41.  
  42.    
  43.  
  44. }
  45.  
  46.  
  47.  
  48. public class AppView extends SurfaceView implements SurfaceHolder.Callback {
  49.  
  50.  
  51.  
  52.         private Graphics goldImage, blueImage2;
  53.  
  54.                
  55.  
  56.         MainClass t2;
  57.  
  58.        
  59.  
  60.         public AppThread _thread;
  61.  
  62.        
  63.  
  64.         private int _x;
  65.  
  66.         private int _y;
  67.  
  68.        
  69.  
  70.         CountDownTimer timer;
  71.  
  72.    
  73.  
  74.         private Map<Integer, Bitmap> _bitmapCache = new HashMap<Integer, Bitmap>();
  75.  
  76.        
  77.  
  78.  
  79.  
  80.         public AppView(Context context) {
  81.  
  82.                 super(context);
  83.  
  84.                 fillBitmapCache();
  85.  
  86.                 getHolder().addCallback(this);
  87.  
  88.                
  89.  
  90.                 _thread = new AppThread(getHolder(), this);
  91.  
  92.                
  93.  
  94.                
  95.  
  96.                 t2 = new MainClass();
  97.  
  98.                
  99.  
  100.                                
  101.  
  102.                 scoreColor.setColor(Color.WHITE);
  103.  
  104.                 scoreColor.setTextSize(30);
  105.  
  106.                
  107.  
  108.                
  109.  
  110.                 goldImage = new Graphics(_bitmapCache.get(R.drawable.targetgold));
  111.  
  112.                 blueImage2 = new Graphics(_bitmapCache.get(R.drawable.targetblue));
  113.  
  114.                
  115.  
  116.                                
  117.  
  118.                 runTimer();
  119.  
  120.         }
  121.  
  122.  
  123.  
  124.        
  125.  
  126.        
  127.  
  128.        
  129.  
  130.         public void runTimer() {
  131.  
  132.                
  133.  
  134.                 timer = new CountDownTimer(6000000, 100) {
  135.  
  136.                         public void onTick(long millisUntilFinished) {
  137.  
  138.                                 if (gameIsRunning==true) {
  139.  
  140.                                                 miliseconds++;
  141.  
  142.                                                 if (miliseconds==10) {
  143.  
  144.                                                         seconds++;
  145.  
  146.                                                         miliseconds=0;
  147.  
  148.                                                 }
  149.  
  150.                                                 if (seconds==60) {
  151.  
  152.                                                         seconds = 0;
  153.  
  154.                                                         minutes++;
  155.  
  156.                                                 }
  157.  
  158.                                                
  159.  
  160.                                                 if (chosenGameLength==seconds) {
  161.  
  162.                                                         doneGame=1;
  163.  
  164.                                                         timer.onFinish();
  165.  
  166.                                                        
  167.  
  168.                                                        
  169.  
  170.                                                        
  171.  
  172.                                                 }
  173.  
  174.                                                
  175.  
  176.                                 }
  177.  
  178.              }
  179.  
  180.              public void onFinish() {
  181.  
  182.                
  183.  
  184.              timer.cancel();
  185.  
  186.            
  187.  
  188.              }
  189.  
  190.                 }.start();
  191.  
  192.        
  193.  
  194.              if (doneGame==1) {
  195.  
  196.                       endGame();
  197.  
  198.              }
  199.  
  200.         }
  201.  
  202.                
  203.  
  204.         public void endGame()  {
  205.  
  206.        
  207.  
  208.                 Intent intent = new Intent(t2, HighScoreScreen.class);
  209.  
  210.                 t2.startActivity(intent);
  211.  
  212.                        
  213.  
  214.                
  215.  
  216.         }
  217.  
  218.        
  219.  
  220.        
  221.  
  222.        
  223.  
  224.         public int getDoneStatus() {
  225.  
  226.                        
  227.  
  228.                 return doneGame;
  229.  
  230.         }
  231.  
  232.  
  233.  
  234.  
  235.  
  236.         public void fillBitmapCache() {
  237.  
  238.                  _bitmapCache.put(R.drawable.targetgold, BitmapFactory.decodeResource(getResources(), R.drawable.targetgold));
  239.  
  240.                  _bitmapCache.put(R.drawable.targetblue, BitmapFactory.decodeResource(getResources(), R.drawable.targetblue));
  241.  
  242.                
  243.  
  244.                  
  245.  
  246.        
  247.  
  248.         }
  249.  
  250.        
  251.  
  252.         @Override
  253.  
  254.         public boolean onTouchEvent (MotionEvent event) {
  255.  
  256.                
  257.  
  258.                
  259.  
  260.                 MainClass tp1 = new MainClass();
  261.  
  262.                 tp1.endGame1();
  263.  
  264.                
  265.  
  266.                
  267.  
  268.                
  269.  
  270.                
  271.  
  272.                 return true;
  273.  
  274.        
  275.  
  276.         }      
  277.  
  278.        
  279.  
  280.                 @Override
  281.  
  282.                 public void onDraw(Canvas canvas) {
  283.  
  284.                
  285.  
  286.                                                
  287.  
  288.                                 Bitmap bitmap;
  289.  
  290.                                 Graphics.Coordinates coords;
  291.  
  292.                                
  293.  
  294.                                 coords = goldImage.getCoordinates();
  295.  
  296.                                 bitmap = goldImage.getGraphic();
  297.  
  298.                                                                                        
  299.  
  300.                                 canvas.drawBitmap(bitmap, coords.getX(),coords.getY(), null);
  301.  
  302.  
  303.  
  304.  
  305.  
  306.                                 Bitmap bitmap3;
  307.  
  308.                                 Graphics.Coordinates coords3;
  309.  
  310.                                 coords3 = blueImage2.getCoordinates();
  311.  
  312.                                 bitmap3 = blueImage2.getGraphic();
  313.  
  314.                                
  315.  
  316.                                 canvas.drawBitmap(bitmap3, coords3.getX(),coords3.getY(), null);
  317.  
  318.                        
  319.  
  320.        
  321.  
  322.         }
  323.  
  324.        
  325.  
  326.        
  327.  
  328.         @Override
  329.  
  330.         public void surfaceChanged(SurfaceHolder holder, int format, int width,
  331.  
  332.                         int height) {
  333.  
  334.                
  335.  
  336.                
  337.  
  338.         }
  339.  
  340.  
  341.  
  342.         @Override
  343.  
  344.         public void surfaceCreated(SurfaceHolder holder) {
  345.  
  346.                
  347.  
  348.                 _thread.setRunning(true);
  349.  
  350.             _thread.start();
  351.  
  352.                
  353.  
  354.         }
  355.  
  356.  
  357.  
  358.         @Override
  359.  
  360.         public void surfaceDestroyed(SurfaceHolder holder) {
  361.  
  362.                 boolean retry = true;
  363.  
  364.             _thread.setRunning(false);
  365.  
  366.             while (retry) {
  367.  
  368.                 try {
  369.  
  370.                     _thread.join();
  371.  
  372.                     retry = false;
  373.  
  374.                 } catch (InterruptedException e) {
  375.  
  376.                  
  377.  
  378.                 }
  379.  
  380.             }
  381.  
  382.                
  383.  
  384.         }      
  385.  
  386.        
  387.  
  388. }
  389.  
  390.  
  391.  
  392. class AppThread extends Thread {
  393.  
  394.         private SurfaceHolder _surfaceHolder;
  395.  
  396.         private AppView _appView;
  397.  
  398.         private boolean _run = false;
  399.  
  400.        
  401.  
  402.        
  403.  
  404.         public AppThread(AppView appView) {
  405.  
  406.                 _AppView = appView;
  407.  
  408.        
  409.  
  410.         }
  411.  
  412.        
  413.  
  414.         public AppThread(SurfaceHolder surfaceHolder, AppView appView) {
  415.  
  416.             _surfaceHolder = surfaceHolder;
  417.  
  418.             _appmView = appView;
  419.  
  420.         }
  421.  
  422.      
  423.  
  424.         public void setRunning(boolean run) {
  425.  
  426.             _run = run;
  427.  
  428.         }
  429.  
  430.      
  431.  
  432.         public SurfaceHolder getSurfaceHolder() {
  433.  
  434.             return _surfaceHolder;
  435.  
  436.         }
  437.  
  438.  
  439.  
  440.         public boolean isRunning() {
  441.  
  442.             return _run;
  443.  
  444.         }
  445.  
  446.         @Override
  447.  
  448.         public void run() {
  449.  
  450.                 Canvas c;
  451.  
  452.             while (_run) {
  453.  
  454.                 c = null;
  455.  
  456.                 try {
  457.  
  458.                     c = _surfaceHolder.lockCanvas(null);
  459.  
  460.                     synchronized (_surfaceHolder) {
  461.  
  462.                         _appView.onDraw(c);
  463.  
  464.                     }
  465.  
  466.                 } finally {
  467.  
  468.                  
  469.  
  470.                     if (c != null) {
  471.  
  472.                         _surfaceHolder.unlockCanvasAndPost(c);
  473.  
  474.                     }
  475.  
  476.                 }
  477.  
  478.             }
  479.  
  480.  
  481.  
  482.         }
  483.  
  484. }
  485.  
  486.  
  487.  
  488.  
Parsed in 0.056 seconds, using GeSHi 1.0.8.4




Thanks so much for your help!
Diddy
kdiddy
Junior Developer
Junior Developer
 
Posts: 19
Joined: Wed Apr 01, 2009 2:30 am

Postby Sowmya » Thu Oct 15, 2009 6:45 am

Hi Diddy,
Check out the following code for Starting an activity from SurfaceView.You cannot call new MainClass() and invoke a new activity with that instance.


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class TestNewView extends Activity {
  2.  
  3.     /** Called when the activity is first created. */
  4.  
  5.         private Preview mPreview;
  6.  
  7.     @Override
  8.  
  9.     public void onCreate(Bundle savedInstanceState) {
  10.  
  11.         super.onCreate(savedInstanceState);        
  12.  
  13.         mPreview = new Preview(this);        
  14.  
  15.         setContentView(mPreview);
  16.  
  17.     }
  18.  
  19. }
  20.  
  21.  
  22.  
  23.  
  24.  
  25. class Preview extends SurfaceView implements SurfaceHolder.Callback {
  26.  
  27.       private Context context;  
  28.  
  29.      
  30.  
  31.     Preview(Context context){
  32.  
  33.      super(context);
  34.  
  35.      this.context=context;    
  36.  
  37.      loadNew();
  38.  
  39.    
  40.  
  41.  
  42.  
  43. }
  44.  
  45.  
  46.  
  47.         @Override
  48.  
  49.         public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
  50.  
  51.                 // TODO Auto-generated method stub
  52.  
  53.                
  54.  
  55.         }
  56.  
  57.  
  58.  
  59.         @Override
  60.  
  61.         public void surfaceCreated(SurfaceHolder holder) {
  62.  
  63.                 // TODO Auto-generated method stub
  64.  
  65.                
  66.  
  67.         }
  68.  
  69.  
  70.  
  71.         @Override
  72.  
  73.         public void surfaceDestroyed(SurfaceHolder holder) {
  74.  
  75.                 // TODO Auto-generated method stub
  76.  
  77.                
  78.  
  79.         }
  80.  
  81.         public void loadNew(){
  82.  
  83.                 Intent in=new Intent();
  84.  
  85.                 in.setClassName("te.ne.vw", "te.ne.vw.NewAct");    
  86.  
  87.      ((Activity) context).startActivity(in);    
  88.  
  89.  
  90.  
  91.  
  92.  
  93.         }
  94.  
  95.        
  96.  
  97. }
Parsed in 0.040 seconds, using GeSHi 1.0.8.4

Run ddms and check out the error if any.

Regards,
Sowmya
Sowmya
Experienced Developer
Experienced Developer
 
Posts: 51
Joined: Fri Mar 06, 2009 7:49 am

Postby kdiddy » Fri Oct 16, 2009 2:30 pm

Hi Sowmya,

That did it!!! Using setClassName was the key.

Thanks so much for all your help. Sorry for not responding last night...I end up being glued to the tv watching the Phillies!!

K
Diddy
kdiddy
Junior Developer
Junior Developer
 
Posts: 19
Joined: Wed Apr 01, 2009 2:30 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 8 guests