button - I'm trying to read in a file on android -
i making app text of button random string, , of random strings stored in .txt file. made method return string , reason whenever run app button blank , there no text on it. know sommething file io because if method return set string "hi" text on button hi. adding oncreate method in case.
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_game); string str = "hello there!"; textview text = (textview) findviewbyid(r.id.question); text.settext(execute()); }
above oncreate below fileio file
public static string execute() { string number[] = new string[100]; double x = math.random()*47; int random = (int) math.round(x); int act = 0; //the try/catch statement encloses code , used handle errors , exceptions might occur try { // read in file bufferedreader in = new bufferedreader(new filereader("activities")); string str; while ((str = in.readline()) != null) { number[act] = str; act++; } in.close(); } catch (ioexception e) { system.out.println("can not open or write file."); } return number[random]; }
you have mentioned try read .txt
file, in filereader
, you've given name called activities
without file extension. that's why nothing getting displayed.
new filereader("activities.txt"); // should
also, either needs relative url
or better, try put file in assets
folder , try read there. that's better , cleaner approach.
check out answer on how read file assets
folder.
Comments
Post a Comment