简体   繁体   中英

How do I pass an intent from an activity to a class extending LinearLayout

I have to pass an Intent value (String) to my class extending LinearLayout. I had tried

Intent in = new Intent(getApplicationContext(), Chat_Layout.class);
in.putExtra("Rid", RequestID); 

by passing the value to the linearlayout class, but can't retrieve the value since it shows undefined error in getIntent().

ReqID = getIntent().getStringExtra("Rid");

You cannot call getIntent() in a ViewGroup ( LinearLayout is a derived class of that). In your extended LinearLayout , you need something like this:

public void setReqId(String rid) {
    this.rid = rid;
}

And from your activity call it with

extendedLinearLayout.setReqId(getIntent().getStringExtra("Rid");

You can do this from your activity when you have a reference to your extended LinearLayout . You can get a reference to it like so in your activity (assuming your class is called ExtendedLinearLayout

ExtendedLinearLayout extendedLinearLayout =
    (ExtendedLinearLayout)findViewById(R.id.extended_linear_layout);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM