加了try catch报错

11-wangcaiwen 2011-07-13 02:44:31
public String getFromAssets(String fileName){
try {
InputStreamReader inputReader = new InputStreamReader(getResources().getAssets().open(fileName) );
BufferedReader bufReader = new BufferedReader(inputReader);
String line="";
String Result="";
while((line = bufReader.readLine()) != null){
Result += line;
return Result;
}
} catch (Exception e) {
e.printStackTrace();
}
}

提示要返回一个字符串类型的结果

This method must return a result of type String

我上面不是有返回值吗?
...全文
233 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
11-wangcaiwen 2011-07-13
  • 打赏
  • 举报
回复
感谢大家~~结贴吧~~
b87936260 2011-07-13
  • 打赏
  • 举报
回复
那就用handler或者AsyncTask异步来处理,具体用法很多资料上有
总之耗时操作就用多线程
11-wangcaiwen 2011-07-13
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 b87936260 的回复:]

在main.xml文件里面给TextView加个Id
android:id="@+id/txt"
然后
TextView text = findViewById(R.id.txt);
setContentView(R.layout.main);
txt.setText(txt);

OK 行了
[/Quote]
确实,我用了这个,可能是我获取book.txt文本时时间过长,导致响应不过来
这样直接读取txt是不是会存在性能问题?
b87936260 2011-07-13
  • 打赏
  • 举报
回复

TextView text = (TextView)findViewById(R.id.txt);
setContentView(R.layout.main);
text.setText(txt);
b87936260 2011-07-13
  • 打赏
  • 举报
回复
在main.xml文件里面给TextView加个Id
android:id="@+id/txt"
然后
TextView text = findViewById(R.id.txt);
setContentView(R.layout.main);
txt.setText(txt);

OK 行了
11-wangcaiwen 2011-07-13
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 hjue 的回复:]

可以在getFromAssets 最后一行加上 return "";

setContentView的参数需要是layout的id ,如 R.layout.main
[/Quote]
是的,我如何让他显示txt = getFromAssets(book);这个txt的值呢?
fontlose 2011-07-13
  • 打赏
  • 举报
回复
while((line = bufReader.readLine()) != null){
Result += line;
//return Result;移下一点
}
return Result;
多鱼的夏天 2011-07-13
  • 打赏
  • 举报
回复
可以在getFromAssets 最后一行加上 return "";

setContentView的参数需要是layout的id ,如 R.layout.main
11-wangcaiwen 2011-07-13
  • 打赏
  • 举报
回复
恩~我在最后加了return "程序出错";就行了
--------------------------------------------

我希望屏幕显示的是我获取Assets里资源的字符串时
String txt;
String book;
book = "book.txt";
txt = getFromAssets(book);
setContentView(txt); '这里为什么不可以呢?
降龙 2011-07-13
  • 打赏
  • 举报
回复
最后加一条return语句
b87936260 2011-07-13
  • 打赏
  • 举报
回复
public String getFromAssets(String fileName){
try {
InputStreamReader inputReader = new InputStreamReader(getResources().getAssets().open(fileName) );
BufferedReader bufReader = new BufferedReader(inputReader);
String line="";
String Result="";
while((line = bufReader.readLine()) != null){
Result += line;
return Result;
}
} catch (Exception e) {
e.printStackTrace();
}
}
循环一次就return?那还要while干嘛?
public String getFromAssets(String fileName){
String Result="";
try {
InputStreamReader inputReader = new InputStreamReader(getResources().getAssets().open(fileName) );
BufferedReader bufReader = new BufferedReader(inputReader);
String line="";
while((line = bufReader.readLine()) != null){
Result += line;

}
} catch (Exception e) {
e.printStackTrace();
}
return Result;
}
这样就可以了
b87936260 2011-07-13
  • 打赏
  • 举报
回复
要是 出现异常 会直接跳转到catch块,你在看你的return 语句,就不会执行了!所以最后还要加一句return

80,360

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧