变量不会从null更改

问题描述 投票:-1回答:1

我的程序是将bname更改为position但不会出于某种原因我将位置发送到Record Expeance这个方式R.id.record_expeance:

    startActivity(new Intent(BudgetStart.this, RecordExpeance.class));
    ex.changeName(position);
    BudgetStart.this.finish();
    break; 

之后应该等于位置但是它仍然是空的我不确定这是不是因为我开始那个新的类或者什么但是我无法弄明白

package budget.app;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class RecordExpeance extends Activity implements OnClickListener
{    
    private String bname;
    protected void onCreate(Bundle savedInstanceState) 
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recordexpeance);
        final BudgetDB entry = new BudgetDB(RecordExpeance.this);
        EditText a =(EditText)findViewById(R.id.txtExpeance);
        EditText b =(EditText)findViewById(R.id.txtEMomo);
        TextView c =(TextView)findViewById(R.id.lblBudgetAmount);
        TextView d =(TextView)findViewById(R.id.lblBudgetName);
       d.setText(bname);

        String bamount;

        entry.open();
        bamount = entry.getBamount(bname);
        entry.close();

        Button A = (Button)findViewById(R.id.btnFinish);

        A.setOnClickListener(this);
    }

    public void onClick(View argo) 
    {
        // TODO Auto-generated method stub
        startActivity(new Intent(RecordExpeance.this, BudgetStart.class));
        RecordExpeance.this.finish();
    }
    public void changeName(String position) 
    {
        // TODO Auto-generated method stub

        bname = position;
    }
}
android android-edittext
1个回答
1
投票

在意图中传递position会更容易:

Intent intent = new Intent(BudgetStart.this, RecordExpeance.class);
intent.putExtra("position", position);
startActivity(intent);
BudgetStart.this.finish();
break; 

然后从RecordExpeance中的Intent中读取它:

super.onCreate(savedInstanceState);
setContentView(R.layout.recordexpeance);

bname = getIntent().getIntExtra("position", 1); 
// Where 1 is the default value if you forgot use putExtra()
© www.soinside.com 2019 - 2024. All rights reserved.