简体   繁体   English

无法以不同方法定义的内部类中引用非最终变量LinearLayout

[英]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. 因为我必须通过LinearLayout和视图才能删除该视图。 I can't make LinearLayout ll and view final as they are being passed new values on run time. 由于无法在运行时传递新值,因此无法制作LinearLayout ll并无法查看最终值。 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: onCreate的方法实现如下:

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限定词即可。 final LinearLayout ll and final View view = layoutInflater... . final LinearLayout llfinal View view = layoutInflater... As long as you're not changing references to the objects, you're totally fine to have them final . 只要您不更改对对象的引用,就可以将它们定为final

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

Solved it ... In case anyone needs solution here it is .... 解决了...如果有人需要解决方案,那就是...。

Declared View view; 声明View view; outside of addControl method and initialized it in addControl method like view = new View(this); 在addControl方法外部,并在addControl方法中将其初始化,例如view = new View(this); .

Then, added in onClick @Override 然后,添加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. 全部完成,很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不能在不同方法中定义的内部类中引用非final变量i - Cannot refer to a non-final variable i inside an inner class defined in a different method 无法在使用其他方法定义的内部类中引用非最终变量pcurl1 - Cannot refer to a non-final variable pcurl1 inside an inner class defined in a different method 无法在以其他方法定义的内部类中引用非最终变量lblNewLabel - Cannot refer to a non-final variable lblNewLabel inside an inner class defined in a different method 不能在不同方法中定义的内部类中引用非final变量str - 如何解决这个问题? - Cannot refer to a non-final variable str inside an inner class defined in a different method - How to fix this? 不能在不同方法中定义的内部类中引用非最终变量名 - Cannot refer to a non-final variable name inside an inner class defined in a different method Android-无法在以其他方法定义的内部类中引用非最终变量测试 - Android - Cannot refer to a non-final variable test inside an inner class defined in a different method 出现错误“无法在用其他方法定义的内部类中引用非最终变量” - Getting error “Cannot refer to a non-final variable inside an inner class defined in a different method” 类型无法引用用其他方法定义的内部类中的非最终变量Asortiment - Type Cannot refer to a non-final variable Asortiment inside an inner class defined in a different method “不能引用在不同方法中定义的内部类中的非最终变量buttonflag” - “Cannot refer to a non-final variable buttonflag inside an inner class defined in a different method” 不能在不同方法中定义的内部类中引用非最终变量 - Cannot refer to a non-final variable inside an inner class defined in a different method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM