【问题标题】:Android: Appending string - getting force close messageAndroid:附加字符串不起作用
【发布时间】:2011-08-29 18:33:48
【问题描述】:

谁能告诉我下面的代码有什么问题? 我是java和android dev的新手,非常感谢! :)

public class GetContentThenAppend extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView tv2;
        tv2 = (TextView) this.findViewById(R.id.thetext);
        tv2.setText("This is the writing program...\n");

        try{
                /*
               File f = new File(Environment.getExternalStorageDirectory()+"/EngagiaDroid/videos.html");
               FileInputStream fileIS = new FileInputStream(f);
               BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS));
               String readString = new String();

               //just reading each line and pass it on the debugger
               int x = 1;
               while((readString = buf.readLine())!= null){
                   tv2.append( Integer.toString(x) + ":> " );
                   tv2.append( readString );
                   tv2.append( "\n" );
                   x++;
               }
                */

            final String entryString = new String("" +
                    "<div stle='color: red; border: this solid red;'>" +
                    "Hey yo this is the new content!!!" +
                    "</div>" +
                    "</div>" +
                    "</div>" +
                    "</body>" +
                    "</html>");

            String htmlFile = Environment.getExternalStorageDirectory()+"/EngagiaDroid/videos.html";
            FileOutputStream fOut = openFileOutput(htmlFile, MODE_APPEND);
            OutputStreamWriter osw = new OutputStreamWriter(fOut);  

            osw.write(entryString);

            osw.flush();
            osw.close();


            } catch (FileNotFoundException e) {
               e.printStackTrace();
            } catch (IOException e){
               e.printStackTrace();
            }
    }
}

我正在尝试通过向其附加一些字符串/html 代码来更新 html 内容。我在尝试运行时收到强制关闭消息。

【问题讨论】:

  • 它在做什么?你希望它做什么?
  • 什么不起作用?你期待你的字符串中有新的行吗?
  • 您正在经历什么?你的问题是什么?你有一个注释掉的代码块,是这个问题吗?
  • 顺便说一句,在这种情况下您不需要执行“new String”(在 Java 中您很少需要这样做)。您可以连接字符串文字。
  • 我更新了问题,谢谢大家的回复! :)

标签: java android string append


【解决方案1】:

我最终使用了另一种方法。我用的是ff代码:

    String TESTSTRING = "<div style='color: blue; border: thin solid red;'>YO!</div>";

    File root = Environment.getExternalStorageDirectory();
    if (root.canWrite()){
        File gpxfile = new File(root, "samplefile.html");
        FileWriter gpxwriter = new FileWriter(gpxfile, true);
        BufferedWriter out = new BufferedWriter(gpxwriter);
        out.write(TESTSTRING);
        out.close();
    }

【讨论】:

    猜你喜欢
    • 2012-03-03
    • 2012-01-02
    • 1970-01-01
    • 1970-01-01
    • 2014-09-18
    • 1970-01-01
    • 1970-01-01
    • 2012-06-15
    • 1970-01-01
    相关资源
    最近更新 更多