[英]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 ll
和final 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.