我尝试保存文件时,我的应用程序停止

时间:2015-07-01 20:21:30

标签: java android save

当我尝试保存文件时,我收到消息:应用程序已停止。 我知道它直到保存才能正常工作。非常感谢任何帮助。

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() + "/");

                        // this is where I think the 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();
                        }

                        // And here it should be 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) {
    }
}

我的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>

1 个答案:

答案 0 :(得分:2)

更改此行:

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

到:

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

在写作结束后不要忘记关闭文件写作者。