TextView
から値を取得して格納するにはどうすればよいString
ですか? ここでは、TextView の値を持つ文字列を作成し、それをファイルに書き込もうとしています。
以下のコードは実行時エラーを示しています。
package com.ralley.par2;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Par2Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView txtData1;
txtData1 = (TextView) findViewById(R.id.txtData1);
try {
File myFile = new File("/sdcard/mysdfile.txt");
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter =
new OutputStreamWriter(fOut);
String s = txtData1.getText().toString();
myOutWriter.write(s);
myOutWriter.close();
fOut.close();
Toast.makeText(getBaseContext(),
"Done writing SD 'mysdfile.txt'",
Toast.LENGTH_SHORT).show();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Logcat エラー:
12-06 16:51:47.794: D/AndroidRuntime(3291): Shutting down VM
12-06 16:51:47.794: W/dalvikvm(3291): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-06 16:51:47.814: E/AndroidRuntime(3291): FATAL EXCEPTION: main
12-06 16:51:47.814: E/AndroidRuntime(3291): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ralley.par2/com.ralley.par2.Par2Activity}: java.lang.NullPointerException
12-06 16:51:47.814: E/AndroidRuntime(3291): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-06 16:51:47.814: E/AndroidRuntime(3291): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-06 16:51:47.814: E/AndroidRuntime(3291): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-06 16:51:47.814: E/AndroidRuntime(3291): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-06 16:51:47.814: E/AndroidRuntime(3291): at android.os.Handler.dispatchMessage(Handler.java:99)
12-06 16:51:47.814: E/AndroidRuntime(3291): at android.os.Looper.loop(Looper.java:123)
12-06 16:51:47.814: E/AndroidRuntime(3291): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-06 16:51:47.814: E/AndroidRuntime(3291): at java.lang.reflect.Method.invokeNative(Native Method)
12-06 16:51:47.814: E/AndroidRuntime(3291): at java.lang.reflect.Method.invoke(Method.java:507)
12-06 16:51:47.814: E/AndroidRuntime(3291): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-06 16:51:47.814: E/AndroidRuntime(3291): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-06 16:51:47.814: E/AndroidRuntime(3291): at dalvik.system.NativeStart.main(Native Method)
12-06 16:51:47.814: E/AndroidRuntime(3291): Caused by: java.lang.NullPointerException
12-06 16:51:47.814: E/AndroidRuntime(3291): at com.ralley.par2.Par2Activity.onCreate(Par2Activity.java:30)
12-06 16:51:47.814: E/AndroidRuntime(3291): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-06 16:51:47.814: E/AndroidRuntime(3291): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-06 16:51:47.814: E/AndroidRuntime(3291): ... 11 more
12-06 16:52:04.874: I/Process(3291): Sending signal. PID: 3291 SIG: 9