i ma trying to save file on the emulator or the phone by asking the user to enter the file name using the alert dialog and then the system will add to the file name the current date and time but the problem is that the system save the file name by null.txt how to fix this problem ???
SignSActivity.java
package com.devleb.idapp;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class SignSoldgerActivity extends Activity {
EditText edit_txt_note;
final Context context = this;
// attribute for the date picker
public String fileName;
String userinputResult;
Button btndatePicker, btn_save_soldger;
TextView txtatePicker;
int year, monthofyear, dayofmonth;
Calendar cal = Calendar.getInstance();
DateFormat dt = DateFormat.getInstance();
DatePickerDialog.OnDateSetListener dpd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_soldger);
edit_txt_note = (EditText) findViewById(R.id.editTxtNote);
btndatePicker = (Button) findViewById(R.id.btnDateTimePicker);
btndatePicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
new DatePickerDialog(SignSoldgerActivity.this, dpd, cal
.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal
.get(Calendar.DAY_OF_MONTH)).show();
}
});
txtatePicker = (TextView) findViewById(R.id.txtDate);
dpd = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, monthOfYear);
cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
txtatePicker.setText(new StringBuilder().append(year + "/")
.append(monthOfYear + "/").append(dayOfMonth));
}
};
btn_save_soldger = (Button) findViewById(R.id.btnSaveSoldger);
btn_save_soldger.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// / for creating a dialog
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.prompts, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView
.findViewById(R.id.editTextDialogUserInput);
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// get user input and set it to result
// edit text
userinputResult = userInput.getText()
.toString();
SimpleDateFormat formatter = new SimpleDateFormat(
"yyyy/MM/dd\\HH:mm:ss");
Date now = new Date();
fileName = formatter.format(now) + "/"
+ userinputResult;
saveFile(fileName);
txtatePicker.setText(fileName);
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}
// / for saving the file on the SD
public void saveFile(String fileName) {
try {
String sdPath = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/" + fileName + ".txt";
File myFile = new File(sdPath);
myFile.createNewFile();
Toast.makeText(getBaseContext(), "the second step in saving file",
Toast.LENGTH_SHORT).show();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
// append or write
myOutWriter.append(edit_txt_note.getText());
myOutWriter.close();
fOut.close();
edit_txt_note.setText("");
Toast.makeText(getBaseContext(), "Done Writing SD" + fileName,
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT)
.show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.sign_soldger, menu);
return true;
}
}
Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.devleb.idapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.devleb.idapp.SplashScreen"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.devleb.idapp.SecondActivity"
android:label="@string/title_activity_second" >
<intent-filter>
<action android:name="com.devleb.idapp.CLEARSCREEN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.devleb.idapp.SignSoldgerActivity"
android:label="@string/title_activity_sign" >
</activity>
<activity
android:name="com.devleb.idapp.SignOfficerActivity"
android:label="@string/title_activity_sign_officer" >
</activity>
<activity
android:name="com.devleb.idapp.RegisterActivity"
android:label="@string/title_activity_register" >
</activity>
</application>
</manifest>
Log Cat
12-24 13:27:09.918: E/AndroidRuntime(1531): FATAL EXCEPTION: main
12-24 13:27:09.918: E/AndroidRuntime(1531): java.lang.RuntimeException: Unable to instantiate application android.app.Application: java.lang.NullPointerException
12-24 13:27:09.918: E/AndroidRuntime(1531): at android.app.LoadedApk.makeApplication(LoadedApk.java:482)
12-24 13:27:09.918: E/AndroidRuntime(1531): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3938)
12-24 13:27:09.918: E/AndroidRuntime(1531): at android.app.ActivityThread.access$1300(ActivityThread.java:123)
12-24 13:27:09.918: E/AndroidRuntime(1531): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1185)
12-24 13:27:09.918: E/AndroidRuntime(1531): at android.os.Handler.dispatchMessage(Handler.java:99)
12-24 13:27:09.918: E/AndroidRuntime(1531): at android.os.Looper.loop(Looper.java:137)
12-24 13:27:09.918: E/AndroidRuntime(1531): at android.app.ActivityThread.main(ActivityThread.java:4424)
12-24 13:27:09.918: E/AndroidRuntime(1531): at java.lang.reflect.Method.invokeNative(Native Method)
12-24 13:27:09.918: E/AndroidRuntime(1531): at java.lang.reflect.Method.invoke(Method.java:511)
12-24 13:27:09.918: E/AndroidRuntime(1531): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-24 13:27:09.918: E/AndroidRuntime(1531): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-24 13:27:09.918: E/AndroidRuntime(1531): at dalvik.system.NativeStart.main(Native Method)
12-24 13:27:09.918: E/AndroidRuntime(1531): Caused by: java.lang.NullPointerException
12-24 13:27:09.918: E/AndroidRuntime(1531): at android.app.LoadedApk.initializeJavaContextClassLoader(LoadedApk.java:362)
12-24 13:27:09.918: E/AndroidRuntime(1531): at android.app.LoadedApk.getClassLoader(LoadedApk.java:305)
12-24 13:27:09.918: E/AndroidRuntime(1531): at android.app.LoadedApk.makeApplication(LoadedApk.java:474)
12-24 13:27:09.918: E/AndroidRuntime(1531): ... 11 more