我的Android应用程序有问题,我想做的是按下ToggleButton,应用程序每两秒写入一个文件Hello(我真正想要的是每2秒将gps位置保存在文件中),ToggleButton 有 id:boto1。
这是 MainActivity.java:
package com.example.gasquefabaixada;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ToggleButton;
public class MainActivity extends ActionBarActivity {
private ToggleButton Boto1;
@Override
protected void onCreate(Bundle savedInstanceState) {
ToggleButton bt = (ToggleButton) findViewById(R.id.boto1);
bt.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
{
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter("mnt/sdcard/andromina.txt"));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while(isChecked==true)
{
Thread.currentThread();
try {
writer.write("HELLO");
writer.newLine();
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
}
}
});
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
程序崩溃!!!
感谢你所做的一切!!