0

我试图在一个活动中的文件中写一些东西,然后我尝试在另一个活动中读取它以显示它。事实上,用户必须输入一次收件人的号码。之后,我会自动为他输入这个收件人的号码......

我不知道我的错误在哪里,我从昨天开始尝试...非常感谢您的帮助..

包 com.example.automatik;

公共类 AddRecipientNumber 扩展活动 {

//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);
}

}

我的第二个活动:

包 com.example.automatik;

公共类 AddMasterNumber 扩展活动 {

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);
}

}

4

1 回答 1

1

您不需要文件来实现您想要实现的目标。

在你的第一个和另一个中使用Intent.putExtraActivitygetIntent().getStringExtra(String)

编辑:

改用 SharefPreferences:http: //developer.android.com/guide/topics/data/data-storage.html#pref

于 2013-05-11T14:59:47.567 回答