java - Edit ViewGroup outside onCreate -
i create viewgroup programatically. add 2 views, game , seekbar. however, if try change seekbar properties within game (which has pointer androidadapter) using, example, parent.seekbar.setvisibility(view.visible)
app crashes.
the parent filed set follows, inside game class:
public class simulation extends applicationadapter implements gesturedetector.gesturelistener{ androidlauncher parent=null; public void setparent( androidlauncher nparent ){ parent = nparent; } public void something(){ if( parent != null ) parent.layout.removeview( parent.seekbar ); } }
i'm missing permission change view? have set/change before editing seekbar? thanks!
public class androidlauncher extends androidapplication { simulation simul; viewgroup layout; seekbar seekbar; shapedrawable thumb; layoutparams lp; @override protected void oncreate (bundle savedinstancestate) { simul = new simulation(); simul.setparent(this); super.oncreate(savedinstancestate); setcontentview( r.layout.main ); layout = (viewgroup) findviewbyid( r.id.panel ); androidapplicationconfiguration config = new androidapplicationconfiguration(); config.usewakelock = true; config.useaccelerometer = false; config.usecompass = false; layout.addview(initializeforview(simul, config)); seekbar = new seekbar(this); seekbar.setmax(100); thumb = new shapedrawable(new ovalshape()); thumb.setintrinsicheight(50); thumb.setintrinsicwidth(50); seekbar.setthumb(thumb); seekbar.setprogress(1); seekbar.setbackground(new colordrawable(android.r.color.transparent)); lp = new layoutparams(600, 50); seekbar.setlayoutparams(lp); seekbar.setonseekbarchangelistener(new onseekbarchangelistener() { public void onstoptrackingtouch(seekbar arg0) { // todo auto-generated method stub system.out.println(".....111......."); } public void onstarttrackingtouch(seekbar arg0) { // todo auto-generated method stub system.out.println(".....222......."); } public void onprogresschanged(seekbar arg0, int arg1, boolean arg2) { // todo auto-generated method stub system.out.println(".....333......." + arg1); } }); layout.addview(seekbar); } }
only main thread can edit views. edit views other classes, must pass reference main thread class using setparent(this)
. callback method should set in main thread class, method called when wish edit views. in method, must guarantee instructions executed main/ui thread, can using runonuithread(...)
.
check following example:
public class androidlauncher extends androidapplication { simulation simul; viewgroup layout; seekbar seekbar; @override protected void oncreate (bundle savedinstancestate) { super.oncreate(savedinstancestate); simul = new simulation(); simul.setparent(this); setcontentview(r.layout.main); layout = (viewgroup) findviewbyid( r.id.panel ); androidapplicationconfiguration config = new androidapplicationconfiguration(); config.usewakelock = true; config.useaccelerometer = false; config.usecompass = false; layout.addview(initializeforview(simul, config)); seekbar = (seekbar) findviewbyid(r.id.seekbar); seekbar.setonseekbarchangelistener(new onseekbarchangelistener() { @override public void onprogresschanged(seekbar seekbar, int progress, boolean fromuser) { system.out.println( progress ); } @override public void onstarttrackingtouch(seekbar seekbar) { } @override public void onstoptrackingtouch(seekbar seekbar) { } }); } public void showseekbar(){ runonuithread(new runnable() { @override public void run() { seekbar.setvisibility( view.visible ); } }); } public void hideseekbar(){ runonuithread(new runnable() { @override public void run() { seekbar.setvisibility( view.gone ); } }); }
the xml file set follows:
<?xml version="1.0" encoding="utf-8"?> <framelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/main" android:orientation="vertical"> <framelayout android:id="@+id/panel" android:layout_width="match_parent" android:layout_height="match_parent"> </framelayout> <seekbar android:layout_width="406dp" android:layout_height="wrap_content" android:id="@+id/seekbar" android:layout_gravity="top|center_horizontal" android:progress="50" android:max="200" android:visibility="gone" android:layout_margintop="30dp" />
Comments
Post a Comment