使用资产从android中读取文件

时间:2015-03-03 18:38:21

标签: java android file android-activity android-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);
}
}

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

使用转义符号制作&#39; *&#39;被split识别,例如s.split("\\*");

答案 2 :(得分:0)

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

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

这将为你完成这项工作。