Android 为什么stringbuffer在此代码中不起作用?

Android 为什么stringbuffer在此代码中不起作用?,android,Android,我必须修改xml.xml文件 第一: <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:paddingBo

我必须修改xml.xml文件

第一:

<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>
为什么它不起作用?? 当我按下按钮1时,我转到第二个xml.file,但textview是空的

像这样使用代码:

          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);
          }

我最好的猜测是程序的运行目录不是您所认为的,openFileInput()抛出了一个IOException,因为它找不到12.txt。您的catch-all-do-nothing块可能隐藏了一个错误。您可能希望记录异常并中止。如果您不关心程序崩溃的异常情况,可以使用以下廉价技巧获得回溯:

catch(Exception e)
{
    throw(new RuntimeException(e));
}
当我编写的代码应该是高标准项目的一部分(而不是一次性实验或其他什么),或者其他人会看到的代码时,我会生成MyIOException,它将RuntimeException子类化(可能通过中间MyRuntimeException),并有一个IOException成员,然后,当我捕获一个IOException时,我抛出一个包装好的MyIOException,它的好处是允许调用方使用基于类的异常分派。

对不起,伙计!! 我再次编辑了它,现在它将运行完美

 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)

         }





        }
试试这个

    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();
    }
试试这段代码。它对我有用。所以它会帮助你。
包com.example.answerquestion;
导入java.io.DataInputStream;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileWriter;
导入java.io.PrintWriter;
导入android.app.Activity;
导入android.os.Bundle;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
公共类MainActivity扩展了活动{
按钮btn;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn=(按钮)findViewById(R.id.btn1);
btn.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
File dir=新文件(getFilesDir().getAbsolutePath());
试一试{
PrintWriter out=新的PrintWriter(新的FileWriter(dir+“/test.txt”);
对于(int i=0;i<20;i++){
out.println(“omg”);
}
out.close();
File File=新文件(getFilesDir().getAbsolutePath(),“/test.txt”);
FileInputStream in=新的FileInputStream(文件);
//FileInputStream fis=null;
最终StringBuffer=新StringBuffer();
//fis=openFileInput(“12.txt”);
DataInputStream dataIO=新的DataInputStream(in);
字符串strLine=null;
如果((strLine=dataIO.readLine())!=null){
buffer.append(strLine);
}
dataIO.close();
in.close();
System.out.print(buffer.toString());
}
捕获(例外e){
}
}
});
}
}


删除try/catch块-或者可能更适用,记录异常。如果有例外,你永远都不会知道。我是一个非常重要的人。。。。你能详细解释一下吗?
试试{throw new Exception(“Oops”);doImportantStuff()}catch(Exception e){}
-
doImportantStuff
从未被调用,但我们从未发现,因为异常被忽略了!现在,我并不是说生成了异常,但如果是,您永远不会知道(并且可能最终得到一个空的StringBuffer)。那么我应该怎么做?尝试从异常中进行适当的恢复-如果您不能(或者即使可以),那么。对于“盲目吞并”异常要非常小心。第一行中的文件名是什么???我的txt.file名称是12.txt如果您要获取文件名,请使用path.12.txt位于我的资产文件夹中。。。。。。所以请使用以下代码:`AssetManager am=context.getAssets();InputStream=am.open(“test.txt”);'然后???txt.settext(is)是否尝试过此代码?mLine无法解析为变量。。。。。。。出现此错误我已编辑答案!!试一下我试过了,但textview是空的。。。。。为什么???我在我的代码中替换了你们的捕获,但当我按下按钮时,出现了这个错误:不幸的是文本已经停止编辑工作。。。但这句台词并不合理。。。。。是记事本问题吗???我如何修复它???@user3410344可能会将您的文本视图宽度更改为match\u parent
android:layout\u width=“match\u parent”
并重试。它不起作用。我可以使用pdf解决吗??我想是记事本problem@user3410344做吧。快乐编码。我也可以用你的pdf编码吗???pdf就像代码中的记事本???
 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)

         }





        }
    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();
    }
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) {  
                    }

                }

        });
    }

}