public class MainActivity extends Activity implements OnTouchListener {
private float x;
private float y;
Rect clearButton = new Rect(300,570,400,620);
Rect resultsButton = new Rect(900,570,1000,620);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyCustomPanel view = new MyCustomPanel(this);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
addContentView(view, params);
view.setOnTouchListener(this);
}
private class MyCustomPanel extends View {
public MyCustomPanel(Context context) {
super(context);
}@Override
public void draw(Canvas canvas) {
Paint paint = new Paint();
paint.setColor(Color.RED);
canvas.drawCircle(x, y, 5, paint);
canvas.drawRect(clearButton, paint);
canvas.drawRect(resultsButton, paint);
}
}
public boolean onTouch(View v, MotionEvent event) {
x = event.getX();
y = event.getY();
if (clearButton.contains((int)x,(int) y)){
deleteFile("recordResults.txt");
}
else if (resultsButton.contains((int) x, (int) y)) {
try{
FileInputStream fis = openFileInput("recordResults.txt");
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuilder results = new StringBuilder();
String resultsToFile;
String line;
while((line = br.readLine()) != null){
results.append(line);
}
resultsToFile = results.toString();
String root = Environment.getExternalStorageDirectory().getAbsolutePath() + "/results.txt";
FileOutputStream fos = openFileOutput(root, MODE_APPEND);
OutputStreamWriter osw = new OutputStreamWriter(fos);
osw.write(resultsToFile);
osw.flush();
osw.close();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else{
try {
printToFile(x, y);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
v.invalidate();
return true;
}
public void printToFile(Float x, Float y) throws IOException {
final String record = new String("x:" + String.valueOf(x) + ", y:" + String.valueOf(y));
FileOutputStream fos = openFileOutput("recordResults.txt", MODE_APPEND);
OutputStreamWriter osw = new OutputStreamWriter(fos);
osw.write(record);
osw.flush();
osw.close();
}
}
我有這段代碼,我試圖將創建的文件複製到不同的位置。每次按下resultsButton Rect時,它都會停止工作。爲什麼是這樣?有沒有更好的方法來解決這個問題?謝謝!在Android中將一個文件傳輸到另一個文件?
編輯(logcat的):
03-09 08:07:44.139: W/System.err(841): java.io.FileNotFoundException: /data/data/com.example.recordresults/files/recordResults.txt: open failed: ENOENT (No such file or directory)
03-09 08:07:44.239: W/System.err(841): at com.example.recordresults.MainActivity.onTouch(MainActivity.java:74)
03-09 08:07:44.508: W/System.err(841): java.io.FileNotFoundException: /data/data/com.example.recordresults/files/recordResults.txt: open failed: ENOENT (No such file or directory)
03-09 08:07:44.553: W/System.err(841): at com.example.recordresults.MainActivity.onTouch(MainActivity.java:74)
03-09 08:07:54.369: E/MessageQueue-JNI(841): at com.example.recordresults.MainActivity.onTouch(MainActivity.java:86)
03-09 08:07:54.429: E/AndroidRuntime(841): at com.example.recordresults.MainActivity.onTouch(MainActivity.java:86)
'它停止工作'。你什麼意思?究竟發生了什麼? – Simon 2013-03-09 07:51:59
我的設備說不幸RecordResults已停止工作。 – Clarissa 2013-03-09 07:56:20
所以請從logcat發佈堆棧跟蹤。只有相關的行請。 – Simon 2013-03-09 07:57:11