0
我寫了一個簡單的文件保存系統。但它不起作用。我之前寫過它,它工作。可能我忘了一些東西。有人能告訴我我忘了什麼嗎? 謝謝! XMLandroid:文件保存系統
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:id="@+id/hello"
/>
<Button android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/btn"
android:text="save"
android:layout_gravity="center_vertical|center_horizontal"/>
</LinearLayout>
活動:
package com.android.savefiletest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class SaveFileTestActivity extends Activity {
/** Called when the activity is first created. */
FileService file;
TextView hello;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button)findViewById(R.id.btn);
hello = (TextView)findViewById(R.id.hello);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
file.save("1.txt", hello.getText().toString());
Toast.makeText(SaveFileTestActivity.this, "success", 1).show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(SaveFileTestActivity.this, "error", 1).show();
}
}
});
}
}
FileService.java
package com.android.savefiletest;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import android.content.Context;
public class FileService{
private Context context;
public FileService(Context context) {
this.context = context;
}
public void save(String filename, String content) throws Exception{
FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_PRIVATE);
outStream.write(content.getBytes());
outStream.close();
}
}
啊.....是的,我真的很愚蠢。非常感謝! :) – nich