我想從Activity1傳遞EditText到Activity2。findViewById()工作在一個活動,但不在其他
活性1代碼:
public void openNextActivity()
{
Intent intent = new Intent("com.abc.xyz.ImageActivity");
EditText myEditText = (EditText)findViewById(R.id.myEditText);
int myEditTextId = myEditText.getId();
//For Test purpose ----- starts
// **Point1: next line of code works fine in this Activity1**
EditText myEditTextTest = (EditText)findViewById(myEditTextId);
//For Test purpose ----- ends
intent.putExtra("myEditText", myEditTextId);
startActivity(intent);
}
活性2代碼:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.comments_detail);
Bundle extras = getIntent().getExtras();
if(extras != null)
{
int myEditTextId = extras.getInt("myEditText");
// Point2: next line of code displays the correct Id
Log.d("tag","myEditTextId"+ myEditTextId);
// Point 3: Next line of code not works in this Activity2
EditText myEditText = (EditText)findViewById(myEditTextId);
if(myEditText != null)
{
Log.d("tag","Not null");
}
else
{
Log.d("tag","null");// **Point4: this condition executes**
}
}
}
的問題是,行:的EditText myEditText =(的EditText)findViewById(myEditTextId);在Activity1中正常工作,但在Activity2中不起作用。
編輯:
注:這兩個活動都使用不同的佈局 感謝您的寶貴時間&幫助。
如果兩個活動使用相同的佈局的setContentView ()那麼它應該是工作。謝謝 – user370305