我正在用Eclipse编写一个Android应用程序,如果我按如下方式编写应用程序,一切正常:
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;
public class HTableActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mymenu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
if (item.getItemId() == R.id.save_item){
save("testo.txt");
return true;
} else if (item.getItemId() == (R.id.load_item)) {
load("testo.txt");
return true;
} else
return false;
}
private void save(String filename){
StringBuffer aux = new StringBuffer();
Writer writer = null; //scrittore
EditText[] textAreas = new EditText[3];
textAreas[0] = (EditText) findViewById(R.id.editText1);
textAreas[1] = (EditText) findViewById(R.id.editText2);
textAreas[2] = (EditText) findViewById(R.id.editText3);
for(EditText et : textAreas){
aux.append( et.getText().toString() + "\n");
}
try{
writer = new OutputStreamWriter(this.openFileOutput(filename, MODE_PRIVATE));
writer.write(aux.toString());
}catch (IOException e){
Log.e("Hash Table", "Impossibile salvare il file", e);
}
finally{
if (writer != null){
try {
writer.close();
} catch (Throwable t) {
Log.e("Hash Table", "Impossibile chiudere il file", t);
}
}
}
}
private void load(String filename){
StringBuffer aux = new StringBuffer();
Reader reader = null;
String[] splits;
int i=0;
EditText[] textAreas = new EditText[3];
textAreas[0] = (EditText) findViewById(R.id.editText1);
textAreas[1] = (EditText) findViewById(R.id.editText2);
textAreas[2] = (EditText) findViewById(R.id.editText3);
try{
reader = new InputStreamReader(openFileInput(filename));
char[] buf = new char[1024];
int len;
while ((len = reader.read(buf)) != -1) {
aux.append(buf, 0, len);
}
splits = aux.toString().split("\\n");
for(EditText et : textAreas){
et.setText(splits[i++]);
}
} catch (FileNotFoundException e) {
Log.e("Hash Table", "File non trovato", e);
} catch (IOException e) {
Log.e("Hash Table", "Impossibile aprire il file", e);
} finally {
if (reader != null) {
try {
reader.close();
} catch (Throwable t) {
Log.e("Hash Table", "Impossibile chiudere il file", t);
}
}
}
}
}
但考虑到以下变量:
EditText[] textAreas = new EditText[3];
textAreas[0] = (EditText) findViewById(R.id.editText1);
textAreas[1] = (EditText) findViewById(R.id.editText2);
textAreas[2] = (EditText) findViewById(R.id.editText3);
在加载方法中出现比在save中出现,我想让它们“全局”如下:
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;
public class HTableActivity extends Activity {
public EditText textAreas;
HTableActivity (){
textAreas = new EditText[3];
textAreas[0] = (EditText) findViewById(R.id.editText1);
textAreas[1] = (EditText) findViewById(R.id.editText2);
textAreas[2] = (EditText) findViewById(R.id.editText3);
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mymenu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
if (item.getItemId() == R.id.save_item){
save("testo.txt");
return true;
} else if (item.getItemId() == (R.id.load_item)) {
load("testo.txt");
return true;
} else
return false;
}
private void save(String filename){
StringBuffer aux = new StringBuffer();
Writer writer = null;
for(EditText et : textAreas){
aux.append( et.getText().toString() + "\n");
}
try{
writer = new OutputStreamWriter(this.openFileOutput(filename, MODE_PRIVATE));
writer.write(aux.toString());
}catch (IOException e){
Log.e("Hash Table", "Impossibile salvare il file", e);
}
finally{
if (writer != null){
try {
writer.close();
} catch (Throwable t) {
Log.e("Hash Table", "Impossibile chiudere il file", t);
}
}
}
}
private void load(String filename){
StringBuffer aux = new StringBuffer();
Reader reader = null;
String[] splits;
int i=0;
try{
reader = new InputStreamReader(openFileInput(filename));
char[] buf = new char[1024];
int len;
while ((len = reader.read(buf)) != -1) {
aux.append(buf, 0, len);
}
splits = aux.toString().split("\\n");
for(EditText et : textAreas){
et.setText(splits[i++]);
}
} catch (FileNotFoundException e) {
Log.e("Hash Table", "File non trovato", e);
} catch (IOException e) {
Log.e("Hash Table", "Impossibile aprire il file", e);
} finally {
if (reader != null) {
try {
reader.close();
} catch (Throwable t) {
Log.e("Hash Table", "Impossibile chiudere il file", t);
}
}
}
}
}
我还添加了权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
在Android清单中,但尽管如此,当我启动应用程序时,我收到以下错误:
E/dalvikvm(384): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
有人可以解释为什么这种代码更改会导致此错误吗?
Full StackTrace:
[2012-05-27 13:11:00 - HTable] ------------------------------
[2012-05-27 13:11:00 - HTable] Android Launch!
[2012-05-27 13:11:00 - HTable] adb is running normally.
[2012-05-27 13:11:00 - HTable] Performing corso.terminali.HTableActivity activity launch
[2012-05-27 13:11:00 - HTable] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'MyDevice1.6'
[2012-05-27 13:11:00 - HTable] Uploading HTable.apk onto device 'emulator-5554'
[2012-05-27 13:11:00 - HTable] Installing HTable.apk...
[2012-05-27 13:11:03 - HTable] Success!
[2012-05-27 13:11:03 - HTable] Starting activity corso.terminali.HTableActivity on device emulator-5554
[2012-05-27 13:11:05 - HTable] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=corso.terminali/.HTableActivity }
我还在与eclipse集成的Android模拟器(1.6版)中实现了512 MB的虚拟SD卡。
我一直在努力,我得出的结论是,您可以在全局范围内声明和初始化数组textareas(EditText textareas EditText&gt; = new [n]),但是您无法执行全局赋值ID,在构造函数中,在方法“public void onCreate&gt;()”中,当方法“save”和“load”为&gt;时会导致错误调用。因此,虽然我不了解原因,但我认为&gt;最好的解决方案是全局声明数组“textareas”,&gt;然后在两种形式'保存'和'加载'中继续分配ID“textareas [n] =(EditText)&gt; findViewById(R.Id.editTextn)”。如果&gt;有人知道为什么会这样,请告诉我。
我一直在努力,我得出的结论是,您可以在全局范围内声明和初始化数组textareas(EditText textareas EditText&gt; = new [n]),但是您无法执行ID的全局分配,在构造函数中,在方法“public void onCreate&gt;()”中,当方法“save”和“load”为&gt;时会导致错误。调用。因此,虽然我不了解原因,但我认为&gt;最好的解决方案是全局声明数组“textareas”,&gt;然后在两种形式'保存'和'加载'中继续分配ID“textareas [n] =(EditText)&gt; findViewById(R.Id.editTextn)”。如果&gt;谁知道为什么会这样,请告诉我。