java - 使用 Assets 从android中的文件中读取

标签 java android file android-activity android-assets

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

我收到此错误消息:

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 类中实现的 Activity com.example.hajjapp.AfterHajj .错误消息告诉您没有 <activity>此类 list 中的元素。另请注意,您粘贴到问题中的代码来自名为 BeforeHajj 的 Java 类。 .

关于java - 使用 Assets 从android中的文件中读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28839452/

相关文章:

c - 如何解决C中读取/写入二进制文件时的段错误

java - 从 Eclipse 调试 JSP

java - 关于在 JNI 中分配的数组

android - 在android中打印阿拉伯字符串

android - 使用安卓设备扫描和打印

android - 修改Android中的返回栈

linux - 如果 block 设备已经格式化,则退出 mkfs 命令

java - 向文件写入/读取一系列不同的序列化对象

java - 输入多个单词时代码失败

java - ClassNotFoundException : org. Glassfish . Jersey .servlet.ServletContainer