我必须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>
我在资产文件夹中有一个名称为12.txt的txt.file 我希望当我在第一个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是空的!!!
答案 0 :(得分:0)
像这段代码一样使用:
FileInputStream fstream = new FileInputStream(Environment.getExternalStorageDirectory()+filenames));
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null)
{
arrayOfarray.add(strLine);
}
答案 1 :(得分:0)
我最好的猜测是程序的运行目录不是你想象的那样,而openFileInput()抛出一个IOException,因为它找不到12.txt。你的无所事事块可能隐藏了一个错误。您可能希望记录异常并中止。如果你不关心程序崩溃的异常情况,你可以使用以下廉价技巧来获得回溯:
catch(Exception e)
{
throw(new RuntimeException(e));
}
当我编写应该是高标准项目的代码(而不是一次性实验或其他东西),或者其他人会看到的代码时,我会使MyIOException成为RuntimeException的子类(可能通过中间MyRuntimeException)并且有一个IOException成员,然后当我捕获IOException时,我抛出一个包装的MyIOException,它的好处是让我的调用者使用基于类的异常调度。
答案 2 :(得分:0)
抱歉老兄!! 我再次编辑它,现在它将完美运行!!
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);
String mLine ="";
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(getAssets().open("12.txt")));
// do reading, usually loop until end of file reading
mLine = reader.readLine();
reader.close();
} catch (IOException e) {
//log the exception
}
txt.setText(mLine)
}
}
答案 3 :(得分:0)
试试这个..
txt=(TextView)findViewById(R.id.textView1);
AssetManager assetManager = getAssets();
InputStream inputStream = null;
try {
inputStream = assetManager.open("12.txt");
BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line);
}
txt.setText(total.toString());
}
catch (IOException e){
e.printStackTrace();
}
答案 4 :(得分:0)
Try this code. it works for me. so it will help you.
package com.example.answerquestion;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.PrintWriter;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.btn1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
File dir = new File(getFilesDir().getAbsolutePath());
try{
PrintWriter out = new PrintWriter(new FileWriter(dir + "/test.txt"));
for (int i = 0; i < 20; i++) {
out.println("omg");
}
out.close();
File file = new File(getFilesDir().getAbsolutePath(), "/test.txt");
FileInputStream in = new FileInputStream(file);
// FileInputStream fis=null;
final StringBuffer buffer= new StringBuffer();
// fis = openFileInput("12.txt");
DataInputStream dataIO = new DataInputStream(in);
String strLine = null;
if ((strLine = dataIO.readLine()) != null) {
buffer.append(strLine);
}
dataIO.close();
in.close();
System.out.print(buffer.toString());
}
catch (Exception e) {
}
}
});
}
}