我正在尝试这样做,以便我的应用程序可以读取我的文本文件中的单词由一个进位输入分隔并从String数组中将它们吐出来。我的应用程序启动然后只是给我一个空白页面,这非常令人沮丧。这是我的代码:
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Timer;
import java.util.Vector;
import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView helloTxt= (TextView)findViewById(R.id.hellotxt);
ArrayList<String> list = new ArrayList<String>();
try {
InputStream is = getResources().openRawResource(R.raw.test);
if (is != null) {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader buffythevampireslayer = new BufferedReader(isr);
String line;
do {
line = buffythevampireslayer.readLine();
list.add(line);
} while (line != null);
}
is.close();
} catch (Exception ex) {
}
String[] wordsArray=new String[list.size()];
list.toArray(wordsArray);
Thread timer=new Thread(); {
for (int c=0;c<list.size();c++){
helloTxt.setText(wordsArray[c]);
System.out.println("TEXTSET");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
timer.start();
}
}
如果有人能提供帮助,我真的很感激,非常感谢!!!
EDIT :::: 在这篇文章中得到一些帮助后,我现在有了工作应用程序!非常感谢!这是新代码:
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Timer;
import java.util.Vector;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.app.Activity;
import android.content.res.AssetManager;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ReadAndUpdateTextTask readAndUpdateTextTask = new ReadAndUpdateTextTask();
readAndUpdateTextTask.execute();
}
class ReadAndUpdateTextTask extends AsyncTask<Void, String, String> {
public String currentString = "";
String line="";
InputStream isr;
@Override
protected void onPreExecute() {
isr = getResources().openRawResource(R.raw.test);
}
@Override
protected String doInBackground(Void... params) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(isr));
while ((line = reader.readLine())!= null) {
currentString += line + "\n";
publishProgress(currentString);
// I don't think you really need this but you want a sleep for 5000 ms
SystemClock.sleep(5000);
}
isr.close();
} catch (Exception ex) {
}
return currentString;
}
@Override
protected void onProgressUpdate(String... currentString) {
TextView helloTxt= (TextView)findViewById(R.id.hellotxt);
helloTxt.setText(currentString[0]);
}
@Override
protected void onPostExecute(String result) {
TextView helloTxt= (TextView)findViewById(R.id.hellotxt);
helloTxt.setText(result);
}
}
}
答案 0 :(得分:0)
我不知道这是否可以解决任何问题,但您可以尝试按照in Oracle's documentation所示声明您的列表。我会稍微看一下。
List<String> list = new ArrayList<String>();
答案 1 :(得分:0)
如果不是拼写错误,你的问题就在这里:
String[] wordsArray=new String[list.size()];
list.toArray(wordsArray);
它应该是:
String[] wordsArray = list.toArray( new String[0] );
否则数组将填充空值
答案 2 :(得分:0)
你有一个空白屏幕,因为你在主UI线程上睡了一觉。请在AsyncTask
中阅读文件并发布其流程。
您的onCreate
方法应如下所示:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ReadAndUpdateTextTask readAndUpdateTextTask = new ReadAndUpdateTextTask();
readAndUpdateTextTask.execute();
}
class ReadAndUpdateTextTask extends AsyncTask<Void, String, String> {
InputStream isr;
@Override
protected void onPreExecute() {
isr = getResources().openRawResource(R.raw.test);
}
@Override
protected String doInBackground(Void... params) {
try {
String currentString = "";
BufferedReader reader = new BufferedReader(new InputStreamReader(isr));
while ((line = reader.readLine())!= null) {
currentString += line + "\n";
publishProgress(currentString);
// I don't think you really need this but you want a sleep for 5000 ms
SystemClock.sleep(5000);
}
isr.close();
} catch (Exception ex) {
}
return currentString;
}
@Override
protected void onProgressUpdate(String... currentString) {
TextView helloTxt= (TextView)findViewById(R.id.hellotxt);
helloTxt.setText(currentString[0]);
}
@Override
protected void onPostExecute(String result) {
TextView helloTxt= (TextView)findViewById(R.id.hellotxt);
helloTxt.setText(result);
}
}
}
答案 3 :(得分:0)
然后您需要调用AssetManager来获取资产文件夹中的资产
AssetManager assetManager = getAssets();
InputStream inputStream = null;
使用try块包围这些语句,以防在指定路径中找不到该文件
inputStream = assetManager.open("texts/sample.txt"); // path is relative to the assets folder
ByteArrayOutputStream bytesOutputStream = new ByteArrayOutputStream();
byte[] bytes = new byte[4096];
int length = 0;
读取字节并将其写入输出流
while((length = inputStream.read(bytes)) > 0)
bytesOutputStream.write(bytes,0,length);
创建一个新的String,使用带有byteOutputStream的构造函数
String yourString = new String(bytesOutputStream.toByteArray(), "UTF8");
Java String类有一个方法“split”,它以正则表达式作为参数
在你的情况下,使用'\ n'代表新行
String[] yourStringArray = yourString.split("\n");
使用try-catch子句(IOException)对所有内容进行环绕,在找不到文件的情况下抛出
您现在可以使用yourStringArray作为
textView.setText(yourStringArray[index]);