不能在不同方法中定义的内部类中引用非final变量LinearLayout

时间:2014-09-01 14:00:00

标签: java android view android-linearlayout inner-classes

我的代码出现了这个错误。我试图在这里提供关于这个主题的建议,但它们对我的情况没有帮助。因为我必须传递LinearLayout并查看能够删除视图。我无法使LinearLayout ll并查看final,因为它们在运行时传递了新值。有什么建议我怎么能实现呢?谢谢你们。

我的方法:

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

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

任何有解释的解决方案都会有所帮助。

3 个答案:

答案 0 :(得分:0)

只需添加final限定符即可。 final LinearLayout llfinal View view = layoutInflater...。只要你没有改变对象的引用,你就可以完全拥有它们final

答案 1 :(得分:0)

在主类中声明您的LinearLayout并在课程中使用它。

答案 2 :(得分:0)

解决了......如果有人需要解决方案,那就是......

在addControl方法之外声明View view;并在addControl方法中初始化它,如view = new View(this);

然后,添加onClick @Override

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

这一切都完成了,很好&amp;细