我对Android很新,我想将GPS显示的坐标保存在文本文件中。我早些时候问过这个问题,但收到了回复。但是,当我尝试代码时遇到空指针异常。有没有人对如何解决这个问题有任何想法? (下面的大部分代码都不是我的)。
链接到我之前的问题:How do I get coordinates from a GPS service into a text file?
当前代码:
public class MainActivity extends AppCompatActivity {
private Button btn_start, btn_stop;
public Button save;
BufferedWriter writer;
private BroadcastReceiver broadcastReceiver;
int delay = 1000; // delay for 1 sec.
int period = 10000; // repeat every 10 sec.
Timer timer = new Timer();
public EditText editText;
public TextView textView;
public String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/aaTutorial";
@Override
protected void onResume() {
super.onResume();
if(broadcastReceiver == null){
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent)
{
textView.append("\n" +intent.getExtras().get("coordinates"));
try {
writer.write((String) intent.getExtras().get("coordinates"));
} catch (IOException e) {
e.printStackTrace();
}
try {
writer.newLine();
} catch (IOException e) {
e.printStackTrace();
}
}
};
};
registerReceiver(broadcastReceiver,new IntentFilter("location_update"));
}
protected void onDestroy() {
super.onDestroy();
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
if(broadcastReceiver != null){
unregisterReceiver(broadcastReceiver);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
writer = new BufferedWriter(new FileWriter("mnt/sdcard/gps.txt"));
} catch (IOException e) {
e.printStackTrace();
}
btn_start = (Button) findViewById(R.id.button);
btn_stop = (Button) findViewById(R.id.button2);
textView = (TextView) findViewById(R.id.textView);
// save = (Button) findViewById(R.id.save);
File dir = new File(path);
dir.mkdirs();
if(!runtime_permissions())
enable_buttons();
}
private void enable_buttons() {
btn_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i =new Intent(getApplicationContext(),GPS_Service.class);
startService(i);
}
});
btn_stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),GPS_Service.class);
stopService(i);
}
});
}
private boolean runtime_permissions() {
if(Build.VERSION.SDK_INT >= 23 && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},100);
return true;
}
return false;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == 100){
if( grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED){
enable_buttons();
}else {
runtime_permissions();
}
}
}
}
错误发生在OnResume
方法下的第一个try / catch中。
用
尝试在logcat中的空对象引用上调用虚方法'void java.io.BufferedWriter.write(java.lang.String)'。
答案 0 :(得分:1)
您的编写器未初始化,因为您尝试写入的文件无效。首先,你离开了开头/
,你的意思是/mnt/sdcard
。其次,没有承诺是SD卡的安装位置。永远不要在android中使用SD卡的直接路径,使用getExternalDirectory
来查找它。第三,在kitkat及以上版本中,由于您没有获得对SD卡根目录的写访问权,因此仍然会失败。