【发布时间】:2014-05-08 19:34:01
【问题描述】:
我要xml.file
第一:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Text1" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="36dp"
android:text="Button1" />
</RelativeLayout>
秒:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="20dp"
android:layout_marginTop="28dp"
android:text="TextView" />
</RelativeLayout>
我在资产文件夹中有一个 txt.file,其名称为 12.txt 我想当我在第一个 xml.file 中按下 button1 时,我会转到第二个 xml.file 和 textview,并在资产文件夹中设置我的 txt.file
代码是:
public class Text2 extends Activity {
private TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.text2);
txt=(TextView)findViewById(R.id.textView1);
FileInputStream fis=null;
final StringBuffer buffer= new StringBuffer();
try {
fis = openFileInput("12.txt");
DataInputStream dataIO = new DataInputStream(fis);
String strLine = null;
if ((strLine = dataIO.readLine()) != null) {
buffer.append(strLine);
}
dataIO.close();
fis.close();
}
catch (Exception e) {
}
txt.setText(buffer.toString());
}
}
为什么它不起作用? 当我按下 button1 时,我转到第二个 xml.file 但 textview 为空!!!
【问题讨论】:
-
删除 try/catch 块 - 或者可能更适用,记录异常。如果有例外,你永远不会知道。
-
我很内向……你能详细解释一下吗?
-
try { throw new Exception("Oops"); doImportantStuff() } catch (Exception e) {}-doImportantStuff从未被调用,但我们从未发现,因为异常被忽略了!现在,我并不是说生成了一个异常,但如果是,你永远不会知道(并且可能以一个空的StringBuffer结束)。 -
那我该怎么办?
-
尝试从异常中适当地恢复 - 如果你不能(或者即使你可以),log the exception。小心“盲目吞咽”异常。
标签: android