ボタンをクリックしたときにこのフラグメントをcsvファイルに書き込むが、java.io.IOExceptionが発生し続ける:オープンに失敗した:ENOENT(そのようなファイルまたはディレクトリはありません)。どんな助けでも大歓迎です。Androidがファイルを作成していない
public class AddFragment extends Fragment {
static EditText spent,saved,coupons;
Button writeExcelButton;
String data;
Spinner spinner;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.add_layout, container, false);
setSpinnerContent(view);
spent = (EditText) view.findViewById(R.id.spent1);
saved = (EditText) view.findViewById(R.id.saved1);
coupons = (EditText) view.findViewById(R.id.coupons1);
writeExcelButton = (Button) view.findViewById(R.id.button_addGroc);
writeExcelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateSheet();
}
});
return view;
}
private void setSpinnerContent (View view) {
spinner = (Spinner) view.findViewById(R.id.groc_store);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
R.array.store1, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
public void updateSheet() {
try {
// This is the string that should be written to file
String mySpin =spinner.getSelectedItem().toString();
data = mySpin + "," + spent.getText().toString() + "," + saved.getText().toString() + "," + coupons.getText().toString() + "/n";
// This is the file that should be written to
String sdCard = Environment.getExternalStorageDirectory().toString();
File dir = new File(sdCard + "/dir");
if (!dir.exists()) {
dir.mkdir();
}
File myFile = new File(dir.getAbsolutePath(), "savings.csv");
// if file doesn't exists, then create it
if (!myFile.exists()) {
myFile.createNewFile();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
これがまたちょうどを渡してみてください
11-14 13:29:23.681: W/System.err(14386): java.io.IOException: open failed: ENOENT (No such file or directory)
11-14 13:29:23.681: W/System.err(14386): at java.io.File.createNewFile(File.java:948)
11-14 13:29:23.691: W/System.err(14386): at com.example.myfirstapp.AddFragment.updateSheet(AddFragment.java:101)
11-14 13:29:23.691: W/System.err(14386): at com.example.myfirstapp.AddFragment$1.onClick(AddFragment.java:59)
11-14 13:29:23.691: W/System.err(14386): at android.view.View.performClick(View.java:4240)
11-14 13:29:23.691: W/System.err(14386): at android.view.View$PerformClick.run(View.java:17721)
11-14 13:29:23.691: W/System.err(14386): at android.os.Handler.handleCallback(Handler.java:730)
11-14 13:29:23.691: W/System.err(14386): at android.os.Handler.dispatchMessage(Handler.java:92)
11-14 13:29:23.691: W/System.err(14386): at android.os.Looper.loop(Looper.java:137)
11-14 13:29:23.691: W/System.err(14386): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-14 13:29:23.691: W/System.err(14386): at java.lang.reflect.Method.invokeNative(Native Method)
11-14 13:29:23.691: W/System.err(14386): at java.lang.reflect.Method.invoke(Method.java:525)
11-14 13:29:23.701: W/System.err(14386): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-14 13:29:23.711: W/System.err(14386): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-14 13:29:23.711: W/System.err(14386): at dalvik.system.NativeStart.main(Native Method)
11-14 13:29:23.711: W/System.err(14386): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
11-14 13:29:23.711: W/System.err(14386): at libcore.io.Posix.open(Native Method)
11-14 13:29:23.711: W/System.err(14386): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
11-14 13:29:23.711: W/System.err(14386): at java.io.File.createNewFile(File.java:941)
あなたのマニフェストに ' 'がありますか? –
kiruwka
ええ、そこにあります。 – user3033922