[英]read from file in android using assets
I am trying to read file from assets in android. 我正在尝试从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);
}
}
Somewhere, you are trying to start an activity that is supposed to be implemented in the Java class com.example.hajjapp.AfterHajj
. 在某个地方,您正在尝试启动应该在Java类
com.example.hajjapp.AfterHajj
实现的活动。 The error message is telling you that there is no <activity>
element in the manifest for this class. 错误消息告诉您清单中没有此类的
<activity>
元素。 Also note that the code you pasted into your question is from a Java class named BeforeHajj
. 还要注意,您粘贴到问题中的代码来自一个名为
BeforeHajj
的Java类。
使用转义符号使'*'被split
识别,例如s.split("\\\\*");
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. 这将为您完成工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.