android - Update SurfaceView by passing though some variables to this Class -


i'm still quite new android , java programming , newer draw images dynamically.

what update line within activity main. keep drawing separate main therefore added new file , class called panel.

when run code fc java.lang.nullpointerexception. when remove draw(); oncreate() don't fc.

so form within main.java calculate values want pass through panel use draw figure.

this simplified version of code, think fundamentally wrong because first time use more 1 java file.

thanks lot help!

main.java (simplified)

package com.tricky_design.app;  import com.tricky_design.app.*;  public class main extends activity {      private panel drawing;      @override     public void oncreate(bundle icicle) {         super.oncreate(icicle);         setcontentview(r.layout.main);          drawing = (panel) findviewbyid (r.id.drawing);         draw();      }      private void draw() {         drawing.redraw( 10, 20, 30, 40);     } } 

panel.java

package com.tricky_design.app;  import android.content.context; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.util.attributeset; import android.view.motionevent; import android.view.surfaceholder; import android.view.surfaceview;  class panel extends surfaceview implements surfaceholder.callback{      private int lineleft    = 0;     private int lineright   = 0;     private int linetop     = 0;     private int linebottom  = 0;      paint paint = new paint();      public panel(context context, attributeset attributeset) {         super(context, attributeset);         getholder().addcallback(this);     }      @override     public void ondraw(canvas canvas) {         drawlines(canvas);     }      public void drawlines(canvas canvas) {         canvas.drawline(left, right, bottom, top, paint);     }      public void redraw(int left, int right, int top, int bottom) {         lineleft   = left;         lineright  = right;         linetop    = top;         linebottom = bottom;     }      @override     public void surfacechanged(surfaceholder holder, int format, int width, int height) {     }      @override     public void surfacecreated(surfaceholder holder) {         canvas c = getholder().lockcanvas();         draw(c);         getholder().unlockcanvasandpost(c);     }      @override     public void surfacedestroyed(surfaceholder holder) {      } } 

layout.xml (simplified)

 <com.tricky_design.app.panel      android:id="@+id/drawing"      android:layout_width="fill_parent"      android:layout_height="fill_parent"      android:background="#fc123456">  </com.tricky_design.app.panel> 

edit 21-07-2011 18:52 - output logcat

07-21 18:48:34.501: error/androidruntime(12385): fatal exception: main 07-21 18:48:34.501: error/androidruntime(12385): java.lang.runtimeexception: unable start activity componentinfo{com.tricky_design.app/com.tricky_design.app.main}: java.lang.nullpointerexception 07-21 18:48:34.501: error/androidruntime(12385):     @ android.app.activitythread.performlaunchactivity(activitythread.java:1816) 07-21 18:48:34.501: error/androidruntime(12385):     @ android.app.activitythread.handlelaunchactivity(activitythread.java:1837) 07-21 18:48:34.501: error/androidruntime(12385):     @ android.app.activitythread.access$1500(activitythread.java:132) 07-21 18:48:34.501: error/androidruntime(12385):     @ android.app.activitythread$h.handlemessage(activitythread.java:1033) 07-21 18:48:34.501: error/androidruntime(12385):     @ android.os.handler.dispatchmessage(handler.java:99) 07-21 18:48:34.501: error/androidruntime(12385):     @ android.os.looper.loop(looper.java:143) 07-21 18:48:34.501: error/androidruntime(12385):     @ android.app.activitythread.main(activitythread.java:4196) 07-21 18:48:34.501: error/androidruntime(12385):     @ java.lang.reflect.method.invokenative(native method) 07-21 18:48:34.501: error/androidruntime(12385):     @ java.lang.reflect.method.invoke(method.java:507) 07-21 18:48:34.501: error/androidruntime(12385):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:839) 07-21 18:48:34.501: error/androidruntime(12385):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:597) 07-21 18:48:34.501: error/androidruntime(12385):     @ dalvik.system.nativestart.main(native method) 07-21 18:48:34.501: error/androidruntime(12385): caused by: java.lang.nullpointerexception 07-21 18:48:34.501: error/androidruntime(12385):     @ com.tricky_design.app.main.draw(main.java:376) 07-21 18:48:34.501: error/androidruntime(12385):     @ com.tricky_design.app.main.init(main.java:311) 07-21 18:48:34.501: error/androidruntime(12385):     @ com.tricky_design.app.main.oncreate(main.java:245) 07-21 18:48:34.501: error/androidruntime(12385):     @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1093) 07-21 18:48:34.501: error/androidruntime(12385):     @ android.app.activitythread.performlaunchactivity(activitythread.java:1780) 07-21 18:48:34.501: error/androidruntime(12385):     ... 11 more 

there several reasons why code posted above not work. example of how surfaceview should work @ lunar lander code in googleapis should come android sdk download. http://developer.android.com/resources/samples/lunarlander/index.html


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -