Reputation: 85
I try to save some data in internal storage on android but I keep getting a NullPointerException I think it has to do with the getFilesDir() I'm using but I'm not sure. Can Some Please help clarify if that is that case and help me write this file to the device. Here the Error message im am getting
01-20 22:11:59.020: E/AndroidRuntime(329): FATAL EXCEPTION: main
01-20 22:11:59.020: E/AndroidRuntime(329): java.lang.NullPointerException
01-20 22:11:59.020: E/AndroidRuntime(329): at android.content.ContextWrapper.getFilesDir(ContextWrapper.java:178)
01-20 22:11:59.020: E/AndroidRuntime(329): at yantz.imageapp4.main.writeElement(main.java:89)
01-20 22:11:59.020: E/AndroidRuntime(329): at yantz.imageapp4.Panel.onTouchEvent(Panel.java:102)
Here is the oncreate of the main class
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout sv = new FrameLayout(this);
LinearLayout ll = new LinearLayout(this);
test = new Panel(this);
test.settest(1) ;
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(test);
sv.addView(ll);
setContentView(sv);}
Here my OnTouch method in the panel class
public boolean onTouchEvent(MotionEvent event) {
mainactivity=new main();
mainactivity.writeElement(new Element(getResources(),(int) event.getX(),(int) event.getY()));
Log.v("Gesture", "is 1 ");
return super.onTouchEvent(event);
}
Here is my writeObject method inside my main class
public void writeElement(Element obj){
Log.v("main", "made it to method writeElement" );
File f = new File(getFilesDir()+FILENAME);
try {
fos = new FileOutputStream(f);
ObjectOutputStream objectwrite = new ObjectOutputStream(fos);
objectwrite.writeObject(obj);
fos.close();
Log.v("main", "file was made File ");
}catch (FileNotFoundException e){
e.printStackTrace();
Log.v("main", "file was not made File not found ");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.v("main", "file was not made File IOException ");
}
}
manifest
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name="yantz.imageapp4.main" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".charactors" android:label="@string/app_name"
android:theme="@android:style/Theme.Black">
<intent-filter>
<action android:name="yantz.imageapp4.charactors" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".main2" android:label="@string/app_name"
android:theme="@android:style/Theme.Black">
<intent-filter>
<action android:name="yantz.imageapp4.main2" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Upvotes: 0
Views: 4492
Reputation: 43412
I know what's wrong. You can't just write
mainactivity=new main();
getFilesDir should get the correct context instance but it doesn't now. Try something like that:
public boolean onTouchEvent(MotionEvent event) {
main.writeElement(new Element(...), this.getContext);
return super.onTouchEvent(event);
}
public static void writeElement(Element obj, Context context){
...
File f = new File(context.getFilesDir(), FILENAME);
...
}
Upvotes: 1
Reputation: 29632
Please try,
Open your AndroidManifest.xml and write following lines after the closing of tag.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
Upvotes: 0