Redrawing of the Android,view

Source: Internet
Author: User
Tags getcolor

============ Problem Description ============


Mars Sudoku made video, page redraw when there is a problem, "unfortunately,shudu08 has stopped." The feeling is that an XML file is not configured well, ask the master to help correct the error, by the way to tell the basic process of Android run. I am a pure small white, master not spray
Enter the interface:
Click on the space:
Error:

The code is as follows:
Game.java
Package com.liuyuan.shudu08;public class game {private final string str  =  "360000000004230800000004200" + "070460003820000014500013020" + "001900000007048300000000045";p rivate &NBSP;INT&NBSP;SUDOKU&NBSP;[]&NBSP;=&NBSP;NEW&NBSP;INT&NBSP;[9*9];p rivate int used[][][] =  new int[9][9][];p ublic game () {sudoku = frompuzzlestring (str); CalculateAllUsedTiles () ;} Private int gettile (int x,int y) {return sudoku[9*y+x];} Public string gettilestring (int x,int y) {int v = gettile (x, y); if (v ==  0) {return  "";} Else{return string.valueof (v);}} Protected int[] frompuzzlestring (STRING&NBSP;SRC) {int[] sudo = new int[ Src.length ()];for (Int i=0;i<src.length (); i++) {Sudo[i] = src.charat (i)  -  ' 0 ';} Return sudo;} Public void calculateallusedtiles () {for (int x=0;x<9;x++) {for (INT&NBSP;Y&NBsp;= 0;y<9;y++) {used[x][y] = calculateusedtiles (x, y);}}} Public int[] getusedtilesbycoor (int x,int y) {return used[x][y];} Public int[] calculateusedtiles (int x,int y) {int c[] = new int[9];for ( int i=0;i<9;i++) {if (i==y) continue;int t = gettile (x,i); if (t!=0) {c[t-1] = t;}} for (int i =0;i<9;i++) {if (i==x) continue;int t =gettile (i,y); if (t!=0) c[t-1] = t ;} int startx =  (X/3) *3;int starty =  (Y/3) *3;for (int i=startx;i<startx+3;i+ +) {for (int j=starty;j<starty+3;j++) {if (i==x && j==y) continue;int t = GetTile (I,J); if (t!=0) c[t-1] = t;}} Int nused =0;for (INT&NBSP;T:C) {if (t!=0) nused++;} Int c1[] = new int[nused];nused=0;for (INT&NBSP;T:C) {if (t!=0) c1[nused++] = t;} RETURN&NBSP;C1;} Protected boolean settileifvalid (int x,int y,iNt value) {int tiles[] = getusedtiles (x, y), if (value!=0) {for (int tile:tiles) {if ( Tile==value) Return false;}} SetTile (X,y,value); Calculateallusedtiles (); return true;} Protected int[] getusedtiles (int x,int y) {return used[x][y];} Private void settile (Int x,int y,int value) {sudoku[y*9 + x] =  Value;}}

Keydialog.java
Package com.liuyuan.shudu08;import android.app.dialog;import android.content.context;import  android.os.bundle;import android.view.view;//This class is used to implement dialog and to implement custom dialog box functions public class  keydialog extends dialog{//is used to hold the object representing the button in the dialog box private final view keys[] = new &NBSP;VIEW[9];p rivate final int used[];p rivate shuduview shuduview;// The second parameter of the constructor holds the number Public keydialog (context context,int[] used)  {super (Context) that the current cell has used; this.used = used;} When a dialog is displayed for the first time, its OnCreate method is called Protected void oncreate (bundle savedinstancestate) { Super.oncreate (savedinstancestate) settitle ("fillable Number:"), Setcontentview (R.layout.keypad); Findviews (); for (int  i = 0;i<used.length;i++) {if (used[i]!=0) {keys[used[i]-1].setvisibility (View.INVISIBLE);}} Set Listener setlisteners () for all buttons in the dialog box; Private void findviews () {Keys[0] = findviewbyid (r.id.keypad_1); keys[1] = fIndviewbyid (r.id.keypad_2); Keys[2] = findviewbyid (r.id.keypad_3); Keys[3] = findviewbyid ( R.id.keypad_4); Keys[4] = findviewbyid (r.id.keypad_5); Keys[5] = findviewbyid (R.id.keypad_6) ; Keys[6] = findviewbyid (r.id.keypad_7); Keys[7] = findviewbyid (R.id.keypad_8);keys[8]  = findviewbyid (r.id.keypad_9);} Notifies the Shuduview object to refresh the entire nine Gongge displayed data Private void returnresult (int tile) {shuduview.setselectedtile (tile) ;//Call the dismiss method to cancel the display of the dialog box. Dismiss ();} Private void setlisteners () {//traverse the entire keys array for (int i=0;i<keys.length;i++) {final int t  = i+1;keys[i].setonclicklistener (New view.onclicklistener ()  {public void  OnClick (view v) {returnresult (t);}});}}}

Mainactivity.java

package com.liuyuan.shudu08;import android.app.activity;import android.os.bundle;import  android.view.menu;import android.view.menuitem;public class mainactivity extends  activity {@Overrideprotected  void oncreate (bundle savedinstancestate)  {super.oncreate (savedinstancestate);//setcontentview (R.layout.activity_main); Setcontentview (New shuduview (This));} @Overridepublic  boolean oncreateoptionsmenu (Menu menu)  {// inflate the menu ; this adds items to the action bar if it is  Present.getmenuinflater (). Inflate (r.menu.main, menu); return true;} @Overridepublic  boolean onoptionsitemselected (Menuitem item)  {// handle action  bar item clicks here. The action bar will// automatically  Handle clicks on the home/up button, so long// as you specify a parent activity in  Androidmanifest.xml.int id = item.getitemid ();if  (id == r.id.action_settings)  {return true;} return super.onoptionsitemselected (item);}}

Shuduview.java

package com.liuyuan.shudu08;import android.content.context;import android.graphics.canvas; import android.graphics.color;import android.graphics.paint;import  Android.graphics.paint.fontmetrics;import android.view.motionevent;import android.view.view;public  class shuduview extends view{//width and height of cell private float width;private float  height;private int selectedX;private int selectedY;private Game game  = new game ();p Ublic shuduview (context context)  {super (Context); Protected void onsizechanged (INT&NBSP;W,INT&NBSP;H,INT&NBSP;OLDW,INT&NBSP;OLDH) {// Calculates the width and height of the cell this.width = w/9f;this.height = h/9f;super.onsizechanged (W,&NBSP;H,&NBSP;OLDW, &NBSP;OLDH);} Protected void ondraw (Canvas canvas) {paint backgroundpaint = new paint (); Backgroundpaint.setcolor (Getresources (). GetColor (R.color.shudu_background)); Canvas.drawrect (0, 0, getwidth (),  getheight (),  backgroundpaint); Paint darkpaint = new paint ();d Arkpaint.setcolor (Getresources (). GetColor (R.color.shudu_ Dark)); Paint hilitepaint = new paint (); Hilitepaint.setcolor (Getresources (). GetColor (R.color.shudu _hilite)); Paint lightpaint = new paint (); Lightpaint.setcolor (Getresources (). GetColor (R.color.shudu_ Light)), for (int i=0;i<9;i++) {canvas.drawline (0, i*height, getwidth (), i*height,  Lightpaint); Canvas.drawline (0, i*height + 1, getwidth (),  i*height + 1,  hilitepaint); Canvas.drawline (I*width, 0, i*width, getheight (),  lightPaint); Canvas.drawline (I*width + 1, 0, i*width + 1, getheight (),  hilitePaint) ;} for (int i = 0;i<9;i++) {if (i%3!=0) {continue;} Canvas.drawline (0, i*height, getwidth (),  i*height, darkpaint); CANvas.drawline (0, i*height + 1, getwidth (),  i*height + 1, hilitepaint); Canvas.drawline (I*width, 0, i*width, getheight (),  darkpaint); Canvas.drawLine (i*width  + 1, 0, i*width + 1, getheight (),  hilitepaint);} Paint numberpaint = new paint (); Numberpaint.setcolor (Color.Black); NumberPaint.setStyle ( Paint.Style.STROKE); numberpaint.settextsize (height*0.75f); numberpaint.settextalign (Paint.Align.CENTER); Fontmetrics fm = numberpaint.getfontmetrics (); float x = width/2;float y  = height/2 -  (fm.ascent + fm.descent)/2;for (int i=0;i<9;i++) {for (int  j=0;j<9;j++) {Canvas.drawtext (game.gettilestring (i, j),  i*width + x, j*height +y, numberpaint);}} Super.ondraw (canvas);} @Overridepublic  boolean ontouchevent (motionevent event)  {if (event.getaction ()!=  Motionevent.aCtion_down) {return super.ontouchevent (event);} selectedx =  (int) (EVENT.GETX ()/width);selectedy =  (int) (Event.gety ()/height); int used [] = game.getusedtilesbycoor (Selectedx, selectedy); Stringbuffer sb = new stringbuffer (); for (int i=0;i<used.length;i++) {sb.append (used [i]);} Keydialog keydialog = new keydialog (GetContext (), used); Keydialog.show (); return true;} Public void setselectedtile (Int tile) {if (Game.settileifvalid (Selectedx,selectedy,tile)) { Invalidate ();}}}

The file in Layout
Dialog.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:orientation=" vertical "        ; <textview android:id= "@+id/usedtextid" android:layout_width= "Fill_parent" android:layout_height= "W Rap_content "android:text=" @string/hello_world "/></linearlayout>

Keypad.xml

<?xml version= "1.0"  encoding= "Utf-8"? ><tablelayout    xmlns:android= " Http://schemas.android.com/apk/res/android "    android:id=" @+id/keypad "     android:orientation= "vertical"     android:layout_width= "wrap_content"      android:layout_height= "Wrap_content"     android:stretchcolumns= "*" >         <TableRow>        < Button android:id= "@+id/keypad_1"  android:text= "1" ></Button>         <button android:id= "@+id/keypad_2"  android:text= "2" ></Button>         <button android:id= "@+id/keypad_3"  android:text= "3" > </Button>    </TableRow>    <TableRow>         <button android:id= "@+id/keypad_4"  android:text= "4" ></Button>         <button android:id= "@+id/keypad_5"  android:text= "5" ></ Button>        <button android:id= "@+id/keypad_6"   android:text= "6" ></Button>    </TableRow>    < Tablerow>        <button android:id= "@+id/keypad_7"   android:text= "7" ></button>        <button android:id= "@+id/keypad_8"  android:text= "8" ></Button>        < Button android:id= "@+id/keypad_9"  android:text= "9" ></button>    </ Tablerow></tablelayout>

The files within values
Colors.xml

<?xml version= "1.0" encoding= "Utf-8"?><resources> <color name= "Shudu_background" > #ffe6f0ff </ color> <color name= "Shudu_hilite" > #ffffffff </color> <color name= "Shudu_light" > #64c6d4ef </ color> <color name= "Shudu_dark" > #6456648f </color></resources>

============ Solution 1============


What is the fault of the newspaper, Logcat to see.

============ Solution 2============


Notifies the Shuduview object to refresh the data displayed for the entire nine Gongge private void returnresult (int tile) {shuduview.setselectedtile (tile);    Call the dismiss method to cancel the display of the dialog box. Dismiss ();}


Shuduview, this is empty.

============ Solution 3============


Did not see the initialization of the Shuduview.

============ Solution 4============


Private Shuduview shuduview=new Shuduview (GetContext ());

Redrawing of the Android,view

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.