@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
Button mBtn1 = (Button) findViewById(R.id.start_Quit_button);
mBtn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("clicks", "You Clicked B1");
Intent i = new Intent(
StartActivity.this,
MainActivity.class);
startActivity(i);
}
});
Button mBtn2 = (Button) findViewById(R.id.start_Next_button);
mBtn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("clicks", "You Clicked B2");
TextView username = (TextView) findViewById(R.id.login_text);
File root = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/ExperimentLog");
if (!root.exists()) {
root.mkdir();
}
Username = username.getText().toString();
save(root, Username);
Intent i = new Intent(
StartActivity.this,
Test1_0.class);
startActivity(i);
}
});
}
public void save(File dir, String string) {
try {
File filepath = new File(dir, Username + ".txt");
FileWriter writer = new FileWriter(filepath);
writer.append(string);
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
這是我的代碼。正如你所看到的,我希望第二個按鈕進入下一個活動並保存從editText start_Next_button獲得的文本。還有一種將字符串寫入創建的文件的保存方法。 但是當我按在AVD按鈕,機器人工作室表明這一點:Android Studio告訴我找不到該文件
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: java.io.FileNotFoundException: /sdcard/ExperimentLog/Name.txt: open failed: ENOENT (No such file or directory)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at libcore.io.IoBridge.open(IoBridge.java:452)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at java.io.FileWriter.<init>(FileWriter.java:42)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at com.android.mick.encodingaidingexperiment.StartActivity.save(StartActivity.java:74)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at com.android.mick.encodingaidingexperiment.StartActivity$2.onClick(StartActivity.java:61)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at android.view.View.performClick(View.java:5198)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at android.view.View$PerformClick.run(View.java:21147)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at android.os.Looper.loop(Looper.java:148)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at java.lang.reflect.Method.invoke(Native Method)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at libcore.io.Posix.open(Native Method)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: at libcore.io.IoBridge.open(IoBridge.java:438)
04-28 10:09:44.242 2379-2379/com.android.mick.encodingaidingexperiment W/System.err: ... 14 more
BTW我用AVD的Galaxy Nexus運行Android 6.0
您添加了「READ_EXTERNAL_STORAGE」和「WRITE_EXTERNAL_STORAGE」權限嗎? –
<?xml version =「1.0」encoding =「utf-8」?> <使用權限android:name =」android.permission.WRITE_EXTERNAL_STORAGE「/> <使用權限android:name =」android.permission.READ_EXTERNAL_STORAGE「/> –
Mick