使用资产从 android 中的文件中读取

read from file in android using assets

我正在尝试从 android 中的资产中读取文件。我希望每个按钮都打印一个段落,例如第一个按钮打印第一段,第二个按钮打印第二段。 我尝试了下面的代码,但它不起作用。

我收到此错误消息:

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) – 

谁能告诉我问题出在哪里?

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);
}
}

在某个地方,您正在尝试启动一个本应在 Java class com.example.hajjapp.AfterHajj 中实现的 activity。错误消息告诉您此 class 的清单中没有 <activity> 元素。另请注意,您粘贴到问题中的代码来自名为 BeforeHajj.

的 Java class

使用转义符号使“*”被 split 识别,例如s.split("\*");

使用换行符分割段落。所有段落均以新行开头,因此请使用

before.split("\\n");

这将为您完成工作。