我正在尝试在一个活动中的文件中写一些东西,然后我尝试在另一个活动中读取它以显示它。 实际上,用户必须输入一次收件人的号码。之后,我将自动为他输入此收件人的号码......
我不知道我的错误在哪里,我从昨天开始尝试... 非常感谢你的帮助..
package com.example.automatik;
公共类AddRecipientNumber扩展了Activity {
//On stocke le n° du destinataire (1)
String FILENAME = "recipientnumber.txt";
String mehmet = "mehmetmehmet";
FileOutputStream fos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_recipient_number);
// Show the Up button in the action bar.
setupActionBar();
Button button = (Button)findViewById(R.id.recipientbutton1);
button.setOnClickListener(new View.OnClickListener() {
@SuppressWarnings("deprecation")
@Override
public void onClick(final View v)
{
EditText textA = (EditText)findViewById(R.id.recipientedit1);
String a = textA.getText().toString();
EditText textB = (EditText)findViewById(R.id.recipientedit2);
String b = textB.getText().toString();
if(a.equals(b))
{
//On stocke le n° du destinataire (2)
try
{
fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(mehmet.getBytes());
fos.close();
}
catch (FileNotFoundException e) { e.printStackTrace(); }
catch (IOException e) { e.printStackTrace(); }
AlertDialog alertdialog = new AlertDialog.Builder(AddRecipientNumber.this).create();
alertdialog.setMessage("You entered these numbers correctly.\nNow, we will move to the second step!");
alertdialog.setButton("Move", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
sendMove(v);
}
});
alertdialog.show();
}
else
{
AlertDialog alertdialog = new AlertDialog.Builder(AddRecipientNumber.this).create();
alertdialog.setMessage("These numbers must be identical. \nPlease try again.");
alertdialog.show();
}
}
});
}
//Une fois que les numéros entrés sont identiques & que l'utilisateur accepte de passer à la 2nde étape, on lance:
public void sendMove(View v)
{
Intent intent = new Intent(this, AddMasterNumber.class);
startActivity(intent);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
和我的第二项活动:
package com.example.automatik;
公共类AddMasterNumber扩展了Activity {
String finall;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_master_number);
// Show the Up button in the action bar.
//setupActionBar();
//On récupère le n° du destinataire stocké
try
{
FileInputStream in = openFileInput("recipientnumber.txt");
StringBuffer fileContent = new StringBuffer("");
byte[] buffer = new byte[1024];
while(in.read(buffer) != -1)
{
fileContent.append(new String(buffer));
}
in.close();
finall = fileContent.toString();
}
catch (FileNotFoundException e) { e.printStackTrace(); }
catch (IOException e) { e.printStackTrace(); }
TextView text = (TextView)findViewById(R.id.recipienttext3);
text.setText(finall);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:1)
您不需要文件来实现目标。
在您的第一个Activity
和getIntent().getStringExtra(String)
中使用Intent.putExtra。
修改强>
改为使用SharefPreferences:http://developer.android.com/guide/topics/data/data-storage.html#pref