Question

I am trying to pass EditText from Activity1 to Activity2.

Activity1 code:

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

Activity2 code:

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**
        }
    }
}

The problem is that the line : EditText myEditText = (EditText)findViewById(myEditTextId); works fine in Activity1 but its not working in Activity2.

EDIT:

Note: Both activities are using different layouts Thanks for your valuable time & help.

Was it helpful?

Solution

The only views you have accessible to you are those in the layout you loaded at the start of Activity 2, i.e those in R.layout.comments_detail. I'm guessing that Activity 1 loads a different layout with its setContentView(..) and it's in that layout where 'myEditText is defined and in scope.

OTHER TIPS

You can't pass a view as an extra. You can pass the string in the view (if that's your purpose).

It seems like you're trying to get the EditText id before it's been assigned:

int myEditTextId = myEditText.getId();
EditText myEditText = (EditText)findViewById(myEditTextId); **// Point1: Works fine**

Try this instead:

EditText myEditText = (EditText)findViewById(myEditTextId); **// Point1: Works fine**
int myEditTextId = myEditText.getId();

Edit

Does the EditText in question even exist in the set layout? (R.layout.comments_detail)

This can not be done.

If your attempt is to manipulate activity 1 from activity 2 then you should be returning something to activity 1 from activity 2. By passing the ID of view you made in activity 1 to activity 2 shouldn't resolve to anything due to nothing being created in activity 2. findViewById is invoked on the current activity. As you didn't set anything in the view there is nothing for it to find.

I think it can work if you use the same layout R.layout.comments_detail in Activity1 and Activity2 because findViewById() return the unique id and this id only belong layout comments_detail

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top