java - Android studio- How to display text page A and B from assets folder in a click of Button1 and Button2 resp -


while executing m getting error "unfortunately app stopped". want open text or html file once button clicked.

mainactivity.java

package com.example.akarsh.aaa;  import android.graphics.drawable.drawable;  import java.io.inputstream;  import android.os.bundle; import android.app.activity; import android.content.res.assetmanager; import android.graphics.drawable.drawable; import android.view.menu; import android.widget.imageview; import android.widget.textview;  public class mainactivity extends activity {      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          textview txtcontent = (textview) findviewbyid(r.id.tv1);         textview txtfilename = (textview) findviewbyid(r.id.tv2);         imageview iv = (imageview) findviewbyid(r.id.iv);          assetmanager assetmanager = getassets();          try {              string[] files = assetmanager.list("files");              (int = 0; < files.length; i++) {                 txtcontent.append("\n files=>" + + "name" + files);             }         } catch (exception e) {             // todo: handle exception              e.printstacktrace();         }          inputstream input;          try {              input = assetmanager.open("text.txt");              int size = input.available();              byte[] buffer = new byte[size];             input.read(buffer);             input.close();              string text = new string(buffer);              txtfilename.settext(text);          } catch (exception e) {             // todo: handle exception             e.printstacktrace();         }          try {              inputstream in = assetmanager.open("icon_android_small.png");              drawable d = drawable.createfromstream( in , null);             iv.setimagedrawable(d);          } catch (exception e) {             // todo: handle exception             return;         }     } } 

you need implement onclick-method on button

find button in activity_main-file , set

android:onclick="onclick" 

then implement onclick in sourcecode of mainactivity. like:

public void onclick(view view){ ...     } 

and in method open file.


Comments