gpt4 book ai didi

java - 当我尝试保存文件时我的应用程序停止

转载 作者:行者123 更新时间:2023-11-29 20:38:54 25 4
gpt4 key购买 nike

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

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>

最佳答案

改变这一行:

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

到:

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

不要忘记在写入结束后关闭文件写入器。

关于java - 当我尝试保存文件时我的应用程序停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31170919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com