java - My application stops when I try to save a file -


when try save file message: application has stopped. know works fine until save. appreciate help.

package no.hjemme.christian.myapplication;  import android.os.environment; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.*; import android.location.*; import android.content.*;  import java.io.file; import java.io.filewriter; import java.io.ioexception; import java.lang.*;  import android.widget.*;  public class mainactivity extends actionbaractivity implements android.location.locationlistener {      arrayadapter<charsequence> adapterkum;     arrayadapter<charsequence> adapterstatus;     arrayadapter<charsequence> adaptermerket;     arrayadapter<charsequence> adaptertype;     private locationmanager locationmanager;     double latitude;     double longitude;     double lati;     double longi;     spinner kum;     spinner status;     spinner merket;     spinner type;     string kum;     string status;     string merket;     string type;     public filewriter filewriter;      @override     public void oncreate(bundle savedinstancestate) {          super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          kum = (spinner)findviewbyid(r.id.kum);         adapterkum = arrayadapter.createfromresource(this,r.array.kum,android.r.layout.simple_spinner_item);         adapterkum.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item);         kum.setadapter(adapterkum);          status = (spinner)findviewbyid(r.id.status);         adapterstatus = arrayadapter.createfromresource(this,r.array.status,android.r.layout.simple_spinner_item);         adapterstatus.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item);         status.setadapter(adapterstatus);          merket = (spinner)findviewbyid(r.id.merket);         adaptermerket = arrayadapter.createfromresource(this,r.array.merket,android.r.layout.simple_spinner_item);         adaptermerket.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item);         merket.setadapter(adaptermerket);          type = (spinner)findviewbyid(r.id.type);         adaptertype = arrayadapter.createfromresource(this,r.array.type,android.r.layout.simple_spinner_item);         adaptertype.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item);         type.setadapter(adaptertype);          locationmanager = (locationmanager) this.getsystemservice(context.location_service);         locationmanager.requestlocationupdates(locationmanager.gps_provider, 3000, 10,                 (locationlistener)this);          button posisjon = (button)findviewbyid(r.id.posisjon);         posisjon.setonclicklistener(                 new button.onclicklistener() {                     public void onclick(view v) {                         lati = latitude;                         longi = longitude;                         textview breddegrad = (textview) findviewbyid(r.id.breddegrad);                         breddegrad.settext(" " + lati);                         textview lengdegrad = (textview) findviewbyid(r.id.lengdegrad);                         lengdegrad.settext(" " + longi);                     }                 }         );         button lagre = (button)findviewbyid(r.id.lagre);         lagre.setonclicklistener(                 new button.onclicklistener(){                     public void onclick(view view){                         kum = kum.getselecteditem().tostring();                         status = status.getselecteditem().tostring();                         merket = merket.getselecteditem().tostring();                         type = type.getselecteditem().tostring();                          file sdcard = environment.getexternalstoragedirectory();                         file dir = new file (sdcard.getabsolutepath() + "/");                          // think problem starts:                          try {                             filewriter = new filewriter(dir +"stikkrenner.txt", true);                         } catch (ioexception e) {                             e.printstacktrace();                         }                         try {                             filewriter.write(kum + " " + status + " " + merket +                                     " " + type + " " + lati + " " + longi + "\r\n");                             filewriter.flush();                             }                         catch(ioexception e){                             e.printstacktrace();                         }                          // , here should ok again;                          toast.maketext(getbasecontext(), "lagret " + "kum: " + kum +                                 " status: " + status + " merket: " + merket + " type: " + type +                                 " lat: " + lati + " long: " + longi, toast.length_long).show();                     }                 }         );     }     public void onlocationchanged(location location) {         latitude = location.getlatitude();         longitude = location.getlongitude();     }     public void onproviderdisabled(string provider) {         toast.maketext(getbasecontext(), "gps turned off ", toast.length_long).show();     }     public void onproviderenabled(string provider) {         toast.maketext(getbasecontext(), "gps turned on ", toast.length_long).show();     }     public void onstatuschanged(string provider, int status, bundle extras) {     } } 

my manifest.xml:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="no.hjemme.christian.myapplication" > <uses-permission android:name="android.permission.access_fine_location" /> <uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.write_external_storage" /> <uses-permission android:name="android.permission.read_external_storage"/> <application     android:allowbackup="true"     android:icon="@mipmap/ic_launcher"     android:label="@string/app_name"     android:theme="@style/apptheme" >     <activity         android:name=".mainactivity"         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>  </manifest> 

change line:

filewriter = new filewriter(dir +"stikkrenner.txt", true); 

to:

filewriter = new filewriter(new file(dir, "stikkrenner.txt"), true); 

and don't forget close filewriter after end of writing.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -