I had write following lines in Manifet file
> <uses-permission
> android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
>
But still I got Exception. (NullPointerException).
My code is as following
package com.micro;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class Final extends Activity
{
TextView widget31;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
widget31 = (TextView)this.findViewById(R.id.widget31);
WriteSettings(this,"My, name is ,Pramod Deore, What is your
name?");
String data = ReadSettings(this);
widget31.setText(data);
}
public void WriteSettings(Context context, String data)
{
FileOutputStream fOut = null;
OutputStreamWriter osw = null;
try{
fOut = openFileOutput("/sdcard/Music/
settings.dat",MODE_PRIVATE);
osw = new OutputStreamWriter(fOut);
osw.write(data);
osw.flush();
Toast.makeText(context, "Settings
saved",Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
e.printStackTrace();
Toast.makeText(context, "Settings not
saved",Toast.LENGTH_SHORT).show();
}
finally {
try {
osw.close();
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String ReadSettings(Context context){
FileInputStream fIn = null;
InputStreamReader isr = null;
char[] inputBuffer = new char[255];
String data = null;
try{
fIn = openFileInput("/sdcard/Music/settings.dat");
isr = new InputStreamReader(fIn);
isr.read(inputBuffer);
data = new String(inputBuffer);
Toast.makeText(context, "Settings
read",Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
e.printStackTrace();
Toast.makeText(context, "Settings not
read",Toast.LENGTH_SHORT).show();
}
finally {
try {
isr.close();
fIn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return data;
}
}
#########################################################################
Manifest file is like
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.micro"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/
app_name">
<activity android:name=".Final"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
#########################################
Logcat gives following error as:
01-28 14:51:09.411: ERROR/AndroidRuntime(813): Uncaught handler:
thread main exiting due to uncaught exception
01-28 14:51:09.442: ERROR/AndroidRuntime(813):
java.lang.RuntimeException: Unable to start activity ComponentInfo
{com.micro/com.micro.Final}: java.lang.NullPointerException
01-28 14:51:09.442: ERROR/AndroidRuntime(813): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:
2268)
01-28 14:51:09.442: ERROR/AndroidRuntime(813): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:
2284)
01-28 14:51:09.442: ERROR/AndroidRuntime(813): at
android.app.ActivityThread.access$1800(ActivityThread.java:112)
01-28 14:51:09.442: ERROR/AndroidRuntime(813): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
01-28 14:51:09.442: ERROR/AndroidRuntime(813): at
android.os.Handler.dispatchMessage(Handler.java:99)
01-28 14:51:09.442: ERROR/AndroidRuntime(813): at
android.os.Looper.loop(Looper.java:123)
01-28 14:51:09.442: ERROR/AndroidRuntime(813): at
android.app.ActivityThread.main(ActivityThread.java:3948)
01-28 14:51:09.442: ERROR/AndroidRuntime(813): at
java.lang.reflect.Method.invokeNative(Native Method)
01-28 14:51:09.442: ERROR/AndroidRuntime(813): at
java.lang.reflect.Method.invoke(Method.java:521)
01-28 14:51:09.442: ERROR/AndroidRuntime(813): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:782)
01-28 14:51:09.442: ERROR/AndroidRuntime(813): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
01-28 14:51:09.442: ERROR/AndroidRuntime(813): at
dalvik.system.NativeStart.main(Native Method)
01-28 14:51:09.442: ERROR/AndroidRuntime(813): Caused by:
java.lang.NullPointerException
01-28 14:51:09.442: ERROR/AndroidRuntime(813): at
com.micro.Final.WriteSettings(Final.java:53)
01-28 14:51:09.442: ERROR/AndroidRuntime(813): at
com.micro.Final.onCreate(Final.java:27)
01-28 14:51:09.442: ERROR/AndroidRuntime(813): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:
1123)
01-28 14:51:09.442: ERROR/AndroidRuntime(813): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:
2231)
01-28 14:51:09.442: ERROR/AndroidRuntime(813): ... 11 more
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en