0

While executing i m getting error like "Unfortunately your app is stopped". I want to open my text or html file once the button is 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");

            for (int i = 0; i < files.length; i++) {
                txtContent.append("\n Files=>" + i + "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;
        }
    }
}
Viktor Bahtev
  • 4,858
  • 2
  • 31
  • 40
akarsh
  • 51
  • 1
  • 6
  • [How to get file read line by line](http://stackoverflow.com/questions/7175161/how-to-get-file-read-line-by-line) – pRaNaY Sep 26 '15 at 10:48

1 Answers1

0

You need to implement the onClick-Method on your button

Find the button in the activity_main-File and set

android:onClick="onClick"

Then implement the onClick in the sourceCode of your MainActivity. Like:

public void onClick(View view){
...
    }

And in this method you open your file.

Patricia
  • 2,885
  • 2
  • 26
  • 32