android - View.SurfaceView, why its member, mSurfaceHolder, returns null from getSurface()? -


i studying android game development these days. come across problem surfaceview\surfaceholder. when read source code of view/surfaceview.java in android sdk 22, confused. following code:

 public class surfaceview extends mockview {     ...         public surfaceholder getholder() {         return msurfaceholder;     }      private surfaceholder msurfaceholder = new surfaceholder() {         ...          @override         public surface getsurface() { return null; }         @override         public canvas lockcanvas() { return null; }         ...      } } 

i know, msurfaceholder.getsurface()\lockcanvas important, returns null! so, think msurfaceholder may dealt other steps. have learned example surfaceview, didn't figout out special steps deal msurfaceholder, example's code following:

  public class surfaceviewtest extends activity {      string tag = "surfaceviewtest";     fastrenderview surfaceview = null;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         //setcontentview(r.layout.activity_surface_view_test);         surfaceview = new fastrenderview(this);         setcontentview(surfaceview);     }      @override     protected void onresume() {         super.onresume();         surfaceview.doresume();     }      @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if           // present.         getmenuinflater().inflate(r.menu.menu_surface_view_test, menu);         return true;     }      @override     public boolean onoptionsitemselected(menuitem item) {         // handle action bar item clicks here. action bar         // automatically handle clicks on home/up button, long         // specify parent activity in androidmanifest.xml.         int id = item.getitemid();          //noinspection simplifiableifstatement         if (id == r.id.action_settings) {             return true;         }          return super.onoptionsitemselected(item);     }      /**      * customized surfaceview class      */     public class fastrenderview extends surfaceview implements runnable {          boolean running = false;         surfaceholder holder = null;         thread thread = null;          public fastrenderview(context context) {             super(context);             // holder             // getholder() returns surfaceholder implementation,             // isn't null, contains nothing.             holder = getholder();             if (holder == null)                 log.e(tag, "failed valid holder");          }          public void doresume() {             thread = new thread(this);             thread.start();             running = true;         }          public random rand = new random();         @override         public void run() {             while (running) {                 // android sdk 22, surfaceview.java, can see,                 // holder's getsurface() returns null.                 // why? returns null, here exception                  // should thrown out!                 if (!holder.getsurface().isvalid()) {                     continue;                 }                  canvas canvas = holder.lockcanvas();                 if (canvas == null) {                     log.e(tag, "get invalid canvas draw");                     continue;                 }                 canvas.drawrgb(                     rand.nextint(255),                      rand.nextint(255),                      rand.nextint(255));                 holder.unlockcanvasandpost(canvas);                  // sleep                 try {                     thread.sleep(1000);                 }                 catch(exception e) {                     e.printstacktrace();                 }             }             log.i(tag, "running ...");         }          public void dopause() {              running = false;             while (true) {                 try {                     thread.join();                 } catch (exception e) {                     e.printstacktrace();                 }             }         }     } }  

thanks in advance!

i'm afraid refer wrong source code. code of surfaceview post layoutlib not real framework source code, source code of surfaceview here


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -