2012-02-24 129 views
-1

当用户点击保存按钮时应该创建一个文本文件,并且内容应该存储在文件中,但是当我这样做时,我的应用程序就会崩溃。我想创建一个文本文件

public class newfile extends Activity { 
    public EditText textBox,textbox2; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.newfile); 
     Button save = (Button) findViewById(R.id.btnSave); 
     save.setOnClickListener(new View.OnClickListener(){ 

      public void onClick(View v) { 
       textBox = (EditText) findViewById(R.id.txtText1); 
       textbox2 = (EditText) findViewById(R.id.fname); 

       String FILENAME = textbox2.getText().toString(); 
       String value = textBox.getText().toString(); 
       File("R.raw",value); 

       FileOutputStream fos=null ; 
       fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); 
       try { 
        fos.write(((String) value).getBytes()); 
        fos.flush(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       try { 
        fos.close(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       }  
      } 

      private void File(String string, String value) { 
       // TODO Auto-generated method stub 
      } 

      private FileOutputStream openFileOutput(String fILENAME, 
        int modePrivate) { 
       // TODO Auto-generated method stub 
       return null; 
      } 
     }); 
    } 
} 

回答

2

openFileOutput返回null?梅比这就是为什么你的应用程序崩溃?

1

删除这条线......

File("R.raw",value); 

删除这些方法...

private void File(String string, String value) { 
    // TODO Auto-generated method stub 

} 

private FileOutputStream openFileOutput(String fILENAME, 
     int modePrivate) { 
    // TODO Auto-generated method stub 
    return null; 
} 

环绕这条线......

fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); 

...与try/catch块是这样的...

try { 
    fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); 
} catch (FileNotFoundException e) { 
    e.printStackTrace(); 
} 
+0

谢谢IHAVE做了上述变化,但我仍然得到月食“这种发射被配置为打开调试角度来看,当它挂起”的对话框,我Wnt信号来创建文件在用户输入文件@ filedata的名称时,他点击保存按钮,该按钮应该通过从xtboxt获取文件名来创建文件。我想将文件存储在我创建的res/raw文件夹中,请帮助我。 – 2012-02-25 08:09:45

+0

您不能在运行时修改'/ res'文件夹的任何子文件夹(或它们包含的任何文件)。 '/ res'文件夹用于保存在构建时打包的东西,基本上是'只读'的。 – Squonk 2012-02-25 08:56:11

+0

MisterSquonk,我该如何解决这个问题 – 2012-02-25 10:24:55

相关问题