android - How to change a textview from a custom view's class -
let's have custom view inside activity , textview
below custom view. change textview
's text once custom view clicked seem null pointer when use findviewbyid()
, how can that?
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.yuvaleliav1gmail.quoridor_ye.mainactivity" android:background="#dd7d23"> <com.yuvaleliav1gmail.quoridor_ye.comboardview android:layout_width="900px" android:layout_height="900px" android:id="@+id/bview" android:background="@drawable/game_board" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" /> <radiogroup android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/radiogroup" android:layout_below="@+id/bview" android:layout_alignparentleft="true" android:layout_alignparentstart="true"> <radiobutton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="move pawn" android:id="@+id/radiopawn" android:checked="true" android:onclick="radiobuttonclick"/> <radiobutton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="set vertical wall" android:id="@+id/verticalrdio" android:checked="false" android:onclick="radiobuttonclick"/> <radiobutton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="set horizontal wall" android:id="@+id/horizontalrdio" android:checked="false" android:onclick="radiobuttonclick"/> </radiogroup> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="your turn" android:id="@+id/turntext34" android:layout_gravity="center_horizontal" android:layout_below="@+id/radiogroup" android:layout_alignleft="@+id/bview" android:layout_alignstart="@+id/bview" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="your walls left: " android:id="@+id/yourwallstext" android:layout_gravity="center_horizontal" android:layout_below="@+id/turntext34" android:layout_alignleft="@+id/turntext34" android:layout_alignstart="@+id/turntext34" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="10" android:id="@+id/yournumtext" android:layout_gravity="center_horizontal" android:layout_aligntop="@+id/yourwallstext" android:layout_torightof="@+id/yourwallstext" android:layout_toendof="@+id/yourwallstext" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="opponent's walls left:" android:id="@+id/opwallstext" android:layout_gravity="center_horizontal" android:layout_below="@+id/yourwallstext" android:layout_alignleft="@+id/yourwallstext" android:layout_alignstart="@+id/yourwallstext" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="10" android:id="@+id/opnumtext" android:layout_gravity="center_horizontal" android:layout_aligntop="@+id/opwallstext" android:layout_torightof="@+id/opwallstext" android:layout_toendof="@+id/opwallstext" /> </relativelayout>
comboardview custom view's class
import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.graphics.point; import android.os.countdowntimer; import android.os.handler; import android.util.attributeset; import android.view.display; import android.view.motionevent; import android.view.view; import android.view.windowmanager; import android.widget.textview; import android.widget.toast; public class comboardview extends view { private static final int rows = 9; private static final int columns = 9; public static context con; paint paint; gameservice game; textview turns; public static point size = new point(); /* * constructor */ public comboardview(context context, attributeset attrs) { super(context, attrs); con = context; paint = new paint(); windowmanager wm = (windowmanager) context.getsystemservice(context.window_service); display display = wm.getdefaultdisplay(); display.getsize(size); turns = (textview) findviewbyid(r.id.turntext34); } /* */ public boolean ontouchevent( motionevent event ) { game = gameservice.getinstance(); int x = (int)event.getx() / 100; int y = (int)event.gety() / 100; if ( event.getaction() != motionevent.action_up ) return true; if(game.movepawn){ if(game.turn % 2 == 0){ if(game.islegalmove(game.ai.mylocation , y * rows + x)){ game.board.clrplayer(game.ai); game.ai.mylocation = y * rows + x; game.board.setplayer(game.ai); if(turns != null){ turns.settext("white's turn"); } game.turn++; } } else{ if(game.islegalmove(game.player.mylocation , y * rows + x)){ game.board.clrplayer(game.player); game.player.mylocation = y * rows + x; game.board.setplayer(game.player); if(turns != null){ turns.settext("black's turn"); } game.turn++; } } } else { if(((int)event.getx() % 100) < 50) x--; if(((int)event.gety() % 100) < 50) y--; if(game.sethwall){ game.board.sethwall(y,x); game.turn++; } else{ game.board.setvwall(y,x); game.turn++; } } return true; } public void restarttimer() { new countdowntimer(3500, 1000) { public void ontick(long millisuntilfinished) { final toast toast = toast.maketext(con, "restarting in: " + millisuntilfinished / 1000, toast.length_long); toast.show(); new handler().postdelayed(new runnable() { @override public void run() { toast.cancel(); } }, 1000); } public void onfinish() { } }.start(); } protected void ondraw(canvas canvas) { paint.setantialias(true); paint.setcolor(color.black); gameservice.getinstance().ondraw(canvas, paint); } }
and game activity's class
import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.radiobutton; import java.util.timer; public class game extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_game); timer timer = new timer(); gameupdatetimer ut = new gameupdatetimer(); ut.boardview = (comboardview)this.findviewbyid(r.id.bview); timer.schedule(ut, 200, 200); radiobutton pawn = (radiobutton)findviewbyid(r.id.radiopawn); radiobutton hwall = (radiobutton)findviewbyid(r.id.horizontalrdio); radiobutton vwall = (radiobutton)findviewbyid(r.id.verticalrdio); final gameservice g = gameservice.getinstance(); pawn.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { g.movepawn = true; g.sethwall = false; }}); hwall.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { g.movepawn = false; g.sethwall = true; } }); vwall.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { g.movepawn = false; g.sethwall = false; } }); } }
you can't call turns = (textview) findviewbyid(r.id.turntext34);
custom view, return null because textview
exist in activity xml , not in comboardview
.
what can do, instantiate textview
in activity, add clicklistener comboardview
.
textview turns; @override protected void oncreate(bundle savedinstancestate) { //... turns = (textview) findviewbyid(r.id.turntext34); ut.boardview.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { turns.settext("comboardview clicked!"); }}); //... }
Comments
Post a Comment