android - Can't read text file from assets with button click -
i've seen question common, after searching couldnt figure out solution problem: goal read simple text file assets when button clicked. i've followed tutorial, adapted project, when button clicked, nothing happens, although file in right place. here's code , in advance:
public class resultactivity extends activity implements view.onclicklistener { button restart; button answers; textview ler; textview msg; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_result); getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); msg = (textview) findviewbyid(r.id.msg); ler=(textview) findviewbyid(r.id.ler); answers=(button) findviewbyid(r.id.answers); answers.setonclicklistener(this); restart=(button) findviewbyid(r.id.restartquiz); restart.setonclicklistener(this); msg.settext("correct answers: " + quizactivity.correct + "wrong answers: " + quizactivity.wrong + " final score " + quizactivity.score); } @override public void onclick(view view) { if (view == restart) { quizactivity.score=0; quizactivity.correct=0; quizactivity.wrong=0; intent = new intent(this, mainactivity.class); startactivity(a); } if(view==answers){ string text=""; try{ inputstream is= getassets().open("file.txt"); int size=is.available(); byte [] buffer=new byte[size]; is.read(buffer); is.close(); }catch (ioexception e){ e.printstacktrace(); } ler.settext(text); } } public void onbackpressed() { } }
this code may class implementation provided android system. allows access application-specific resources , classes. return assetmanager instance application's package.
public class resultactivity extends activity implements view.onclicklistener { button restart; button answers; textview ler; textview msg; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_result); getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); msg = (textview) findviewbyid(r.id.msg); ler=(textview) findviewbyid(r.id.ler); answers=(button) findviewbyid(r.id.answers); answers.setonclicklistener(this); restart=(button) findviewbyid(r.id.restartquiz); restart.setonclicklistener(this); msg.settext("correct answers: " + quizactivity.correct + "wrong answers: " + quizactivity.wrong + " final score " + quizactivity.score); } @override public void onclick(view view) { if (view == restart) { quizactivity.score=0; quizactivity.correct=0; quizactivity.wrong=0; intent = new intent(this, mainactivity.class); startactivity(a); } if(view==answers){ string text=""; try{ assetmanager text = mycontext.getassets(); inputstream = text.open("file.txt"); inputstream is= getassets().open("file.txt"); int size=is.available(); byte [] buffer=new byte[size]; is.read(buffer); text = new string(buffer, 0, size); //this line missing is.close(); }catch (ioexception e){ e.printstacktrace(); } ler.settext(text); } } public void onbackpressed() { } }
Comments
Post a Comment