graphics - Android:Crash: Binary XML file line : Error inflating class (using SurfaceView) -


i'm having android surfaceview , in i'm trying add buttons this. in surfaceview canvas draw something. , have thread class keep drawing.

package com.androidsurfaceview;  import android.app.activity; import android.os.bundle; import android.view.view; import android.widget.button;  public class androidsurfaceview extends activity {     /** called when activity first created. */     @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.main);          button buttonshowhide = (button)findviewbyid(r.id.showhide);         final button buttondummy = (button)findviewbyid(r.id.dummy);          buttonshowhide.setonclicklistener(                 new button.onclicklistener(){                      @override                     public void onclick(view arg0) {                         // todo auto-generated method stub                         if(buttondummy.getvisibility()==view.visible){                             buttondummy.setvisibility(view.gone);                         }                         else{                             buttondummy.setvisibility(view.visible);                         }                     }                  }         ); 

the thread class

package com.androidsurfaceview;  import android.graphics.canvas; import android.view.surfaceholder; public class mysurfacethread extends thread {     private surfaceholder mythreadsurfaceholder;     private com.androidsurfaceview.test.mysurfaceview  mythreadsurfaceview;     private boolean mythreadrun = false;      public mysurfacethread(surfaceholder surfaceholder, com.androidsurfaceview.test.mysurfaceview surfaceview) {         mythreadsurfaceholder = surfaceholder;         mythreadsurfaceview = surfaceview;     }      public void setrunning(boolean b) {         mythreadrun = b;     }      @override     public void run() {         // todo auto-generated method stub         while(mythreadrun){             canvas c = null;              try{                 c = mythreadsurfaceholder.lockcanvas(null);                 synchronized (mythreadsurfaceholder){                     mythreadsurfaceview.ondraw(c);                 }                 sleep(100);             } catch (interruptedexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             }             finally{                 // in if exception thrown                 // during above, don't leave surface in                 // inconsistent state                 if (c != null) {                     mythreadsurfaceholder.unlockcanvasandpost(c);                 }             }         }     } } 

the surfaceview & class drawing

 package com.androidsurfaceview;  import android.app.activity; import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.util.attributeset; import android.view.surfaceholder; import android.view.surfaceview;   public class test extends activity{        //     ......        //   few things here... class test.      public class mysurfaceview extends surfaceview implements surfaceholder.callback{          private mysurfacethread thread;         private paint paint = new paint(paint.anti_alias_flag);         int cx, cy, offx, offy;          public mysurfaceview(context context) {             super(context);             // todo auto-generated constructor stub             init();         }          public mysurfaceview(context context, attributeset attrs) {             super(context, attrs);             // todo auto-generated constructor stub             init();         }          public mysurfaceview(context context, attributeset attrs, int defstyle) {             super(context, attrs, defstyle);             // todo auto-generated constructor stub             init();         }           private void init(){               getholder().addcallback(this);               thread = new mysurfacethread(getholder(), this);                setfocusable(true); // make sure key events                paint.setstyle(paint.style.stroke);               paint.setstrokewidth(3);               paint.setcolor(color.white);                cx = 0;               cy = 0;               offx = 10;               offy = 10;               }          @override         public void surfacechanged(surfaceholder arg0, int arg1, int arg2, int arg3) {             // todo auto-generated method stub          }          @override         public void surfacecreated(surfaceholder holder) {             // todo auto-generated method stub             thread.setrunning(true);             thread.start();          }          @override         public void surfacedestroyed(surfaceholder holder) {             // todo auto-generated method stub             boolean retry = true;             thread.setrunning(false);             while (retry) {                 try {                     thread.join();                     retry = false;                 }                  catch (interruptedexception e) {                 }             }         }     ////just simple graphic of moving circle.         @override         protected void ondraw(canvas canvas) {             // todo auto-generated method stub             canvas.drawrgb(0, 0, 0);             canvas.drawcircle(cx, cy, 3, paint);             cx += offx;             if (cx > getwidth() || (cx < 0)){                 offx *= -1;                 cx += offx;             }              cy += offy;             if (cy > getheight() || (cy < 0)){                 offy *= -1;                 cy += offy;             }         }     }     } 

here main.xml

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"     android:layout_width="fill_parent"     android:layout_height="fill_parent"> <button     android:id="@+id/showhide"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="toggle button show/hide" /> <button     android:id="@+id/dummy"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="a button" /> <framelayout      android:layout_width="fill_parent"     android:layout_height="fill_parent">  <com.androidsurfaceview.test.mysurfaceview     android:layout_width="fill_parent"     android:layout_height="fill_parent"/>  </framelayout> </linearlayout> 

problem: above code works only if dont have "mysurfaceview" nested class. outer class "test" following error. if remove "class test" works fine.

error/crash

04-29 11:43:18.977: error/androidruntime(21832): fatal exception: main 04-29 11:43:18.977: error/androidruntime(21832): java.lang.runtimeexception: unable start activity componentinfo{com.androidsurfaceview/com.androidsurfaceview.androidsurfaceview}: **android.view.inflateexception: binary xml file line #20: error inflating class com.androidsurfaceview.test.mysurfaceview** 04-29 11:43:18.977: error/androidruntime(21832):     @ android.app.activitythread.performlaunchactivity(activitythread.java:2663) 04-29 11:43:18.977: error/androidruntime(21832):     @ android.app.activitythread.handlelaunchactivity(activitythread.java:2679) 04-29 11:43:18.977: error/androidruntime(21832):     @ android.app.activitythread.access$2300(activitythread.java:125) 04-29 11:43:18.977: error/androidruntime(21832):     @ android.app.activitythread$h.handlemessage(activitythread.java:2033) 04-29 11:43:18.977: error/androidruntime(21832):     @ android.os.handler.dispatchmessage(handler.java:99) 04-29 11:43:18.977: error/androidruntime(21832):     @ android.os.looper.loop(looper.java:123) 04-29 11:43:18.977: error/androidruntime(21832):     @ android.app.activitythread.main(activitythread.java:4627) 04-29 11:43:18.977: error/androidruntime(21832):     @ java.lang.reflect.method.invokenative(native method) 04-29 11:43:18.977: error/androidruntime(21832):     @ java.lang.reflect.method.invoke(method.java:521) 04-29 11:43:18.977: error/androidruntime(21832):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:868) 04-29 11:43:18.977: error/androidruntime(21832):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:626) 04-29 11:43:18.977: error/androidruntime(21832):     @ dalvik.system.nativestart.main(native method) **04-29 11:43:18.977: error/androidruntime(21832): caused by: android.view.inflateexception: binary xml file line #20: error inflating class com.androidsurfaceview.test.mysurfaceview** 04-29 11:43:18.977: error/androidruntime(21832):     @ android.view.layoutinflater.createviewfromtag(layoutinflater.java:576) 04-29 11:43:18.977: error/androidruntime(21832):     @ android.view.layoutinflater.rinflate(layoutinflater.java:618) 04-29 11:43:18.977: error/androidruntime(21832):     @ android.view.layoutinflater.rinflate(layoutinflater.java:621) 04-29 11:43:18.977: error/androidruntime(21832):     @ android.view.layoutinflater.inflate(layoutinflater.java:407) 04-29 11:43:18.977: error/androidruntime(21832):     @ android.view.layoutinflater.inflate(layoutinflater.java:320) 04-29 11:43:18.977: error/androidruntime(21832):     @ android.view.layoutinflater.inflate(layoutinflater.java:276) 04-29 11:43:18.977: error/androidruntime(21832):     @ com.android.internal.policy.impl.phonewindow.setcontentview(phonewindow.java:200) 04-29 11:43:18.977: error/androidruntime(21832):     @ android.app.activity.setcontentview(activity.java:1647) 04-29 11:43:18.977: error/androidruntime(21832):     @ com.androidsurfaceview.androidsurfaceview.oncreate(androidsurfaceview.java:13) 04-29 11:43:18.977: error/androidruntime(21832):     @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1047) 04-29 11:43:18.977: error/androidruntime(21832):     @ android.app.activitythread.performlaunchactivity(activitythread.java:2627) 

any great ...i'm stuck this.

first, must declare view static type, able inflated when no instance of holding class available:

public static class mysurfaceview extends surfaceview      implements surfaceholder.callback 

the line

<com.androidsurfaceview.test.mysurfaceview 

in layout xml suggests, mysurfaceview class inside com.androidsurfaceview.test package, , tries inflate there, wrong.

in layout should follow package.class$innerclass form of declaration.

but since "$" illegal character, cannot write

<com.androidsurfaceview.test$mysurfaceview 

so must specify view in layout xml follows:

<view class="com.androidsurfaceview.test$mysurfaceview"     android:layout_width="fill_parent"     android:layout_height="fill_parent"/> 

this way work.


Comments