简体   繁体   中英

Cannot refer to a non-final variable LinearLayout inside an inner class defined in a different method

I'm getting this error with my code. I tried to implement suggestions on this topic available here but they are not helping in my case. As i have to pass the LinearLayout and view to be able to delete the view. I can't make LinearLayout ll and view final as they are being passed new values on run time. Any suggestions how can I achieve it? Thanks guys.

My method:

private void addControls(String name, LinearLayout ll, String namevalue) {
        View view = new View(this);
        //getBaseContext();
        LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = layoutInflater.inflate(R.layout.edit_phone, null);
        EditText edit_phone = (EditText) view.findViewById(R.id.txtEditPhone);
        edit_phone.requestFocus();
        edit_phone.setText(name, TextView.BufferType.NORMAL);
        TextView nametext = (TextView) view.findViewById(R.id.NameTextView);
        nametext.setText(namevalue, TextView.BufferType.NORMAL);
        ImageButton imagedelete = (ImageButton) view.findViewById(R.id.CancelButton);
        imagedelete.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                ll.removeView(view);
            }
        }) ;
        ll.addView(view);
    }

Method Implementation at onCreate like:

if (memberModel.email.toLowerCase().equals(email))
        {
            selPer = memberModel.name;
            getActionBar().setTitle(selPer);
            if(tNumList.size() >= 1)
            {
                for (int i = 0; i < tNumList.size(); i++)
                {
                    String officePhone = tNumList.get(i).telephoneNumber;
                    LinearLayout telephoneListView = (LinearLayout) findViewById(R.id.phoneHolder);
                    addControls(officePhone, telephoneListView, "Phone");
                }
            }
            if(mNumList.size() >= 1)
            {
                for (int i = 0; i < mNumList.size(); i++)
                {
                    String cellPhone = mNumList.get(i).mobileNo;
                    LinearLayout mobileListView = (LinearLayout) findViewById(R.id.mobileHolder);
                    addControls(cellPhone, mobileListView, "Mobile");
                }
            }
            txtOfficeAddress.setText(memberModel.officeAddress, TextView.BufferType.NORMAL);
            txtHomeAddress.setText(memberModel.homeAddress, TextView.BufferType.NORMAL);
        }
        valueTextView.setText(email);
        ImageButton imageAddPhone = (ImageButton) findViewById(R.id.AddButton);
        imageAddPhone.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                LinearLayout telephoneListView = (LinearLayout) findViewById(R.id.phoneHolder);
                addControls(null, telephoneListView, "Phone");
            }
        }) ;
        ImageButton imageAddMobile = (ImageButton) findViewById(R.id.AddButton1);
        imageAddMobile.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                LinearLayout MobileListView = (LinearLayout) findViewById(R.id.mobileHolder);
                addControls(null, MobileListView, "Mobile");
            }
        }) ;

Any solution with explanation would be helpful.

Just add final qualifiers. final LinearLayout ll and final View view = layoutInflater... . As long as you're not changing references to the objects, you're totally fine to have them final .

在主类中声明您的LinearLayout并在整个类中使用它。

Solved it ... In case anyone needs solution here it is ....

Declared View view; outside of addControl method and initialized it in addControl method like view = new View(this); .

Then, added in onClick @Override

LinearLayout parent = (LinearLayout)v.getParent();
                LinearLayout grandParent=(LinearLayout)parent.getParent();
                grandParent.removeView(parent); in place of ll.removeView(view);

That's it all done, nice & fine.

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