user3057957
user3057957

Reputation:

read from file in android using assets

I am trying to read file from assets in android. I want every button to print a paragraph, for example the first button prints the first paragraph and the second button print the second paragraph. I tried with the code below but its not working.

I get this error message:

03-03 18:40:17.800: E/AndroidRuntime(977): FATAL EXCEPTION: main 
03-03 18:40:17.800: E/AndroidRuntime(977): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.example.hajjapp.AfterHajj } 
03-03 18:40:17.800: E/AndroidRuntime(977): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1622) 
03-03 18:40:17.800: E/AndroidRuntime(977): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417) 
03-03 18:40:17.800: E/AndroidRuntime(977): at android.app.Activity.startActivityForResult(Activity.java:3370) – 

Can anyone tell me where the problem is?

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

  import android.support.v7.app.ActionBarActivity;
 import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Dialog;
import android.content.res.AssetManager;

 public class BeforeHajj extends ActionBarActivity {
  Button whatHajj;
  TextView text;
  Button hajjTime;
  String before;
  String [] s;
  String timeHajj="";
  @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_before_hajj);

    whatHajj = (Button)findViewById(R.id.whatisHajj);
    hajjTime = (Button)findViewById(R.id.hajjTime);
    text =  (TextView)findViewById(R.id.text);


    whatHajj.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try
        {


            AssetManager assetmanager=getAssets();
            InputStream input=assetmanager.open("beforehaj.txt");
            BufferedReader br=new BufferedReader(new InputStreamReader(input));

            String st="";
            StringBuilder sb=new StringBuilder();
            while((st=br.readLine())!=null)
            {

                sb.append(st);


                before+=sb.toString();
                s=before.split("*");
            }
            text.setText(before);
        }
        catch(IOException ep) {
            text.append(ep.toString());
            }

        }
    });




    hajjTime.setOnClickListener(new View.OnClickListener() {

    public void onClick(View view) {
    // custom dialog

   try
        {


            AssetManager assetmanager=getAssets();
            InputStream input=assetmanager.open("beforehaj.txt");
            BufferedReader br=new BufferedReader(new InputStreamReader(input));

            String st="";
            StringBuilder sb=new StringBuilder();
            while((st=br.readLine())!=null){
                sb.append(st);
                before+=sb.toString();
                s=before.split("*");
            }
        }
            catch(IOException ep) {
                text.append(ep.toString());
            }
        final Dialog dialog = new Dialog(BeforeHajj.this);
        dialog.setContentView(R.layout.guide_dialog);
        dialog.setTitle("Hajj Time");

        // set the custom dialog components - text, image and button
        TextView text = (TextView) dialog.findViewById(R.id.dialog_text);
        text.append(s[0]);
        ImageView image = (ImageView) dialog.findViewById(R.id.dialog_image);
        image.setImageResource(R.drawable.dolheja);
        Button dialogButton = (Button)  dialog.findViewById(R.id.dialog_button);
        // if button is clicked, close the custom dialog

        dialogButton.setOnClickListener(new View.OnClickListener() {


                @Override
                public void onClick(View view) {
                    dialog.dismiss();

                }
            });

            dialog.show();
          }
        });
  }







 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.before_hajj, menu);
    return true;
}

@Override
public 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);
}
}

Upvotes: 1

Views: 468

Answers (3)

Iqbal S
Iqbal S

Reputation: 1154

Use new line character to split the paragraphs. All paragraphs start with a new line, so use

before.split("\\\n");

This shall do the job for you.

Upvotes: 0

Iqbal S
Iqbal S

Reputation: 1154

Use escape symbols to make the '*' be recognized by split, e.g. s.split("\\*");

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006554

Somewhere, you are trying to start an activity that is supposed to be implemented in the Java class com.example.hajjapp.AfterHajj. The error message is telling you that there is no <activity> element in the manifest for this class. Also note that the code you pasted into your question is from a Java class named BeforeHajj.

Upvotes: 1

Related Questions