あるアクティビティでファイルに何かを書き込もうとしていますが、別のアクティビティでそれを読み取って表示しようとした後です。実際、ユーザーは受信者の番号を 1 回入力する必要があります。その後、この受信者の番号を自動的に入力します...
私のエラーがどこにあるのかわかりません。昨日から試してみました... ありがとうございました..
パッケージcom.example.automatik;
public class AddRecipientNumber extends 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);
}
}
そして私の2番目の活動:
パッケージcom.example.automatik;
public class AddMasterNumber extends 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);
}
}