开发者

ViewStub'parent viewgroup

开发者 https://www.devze.com 2023-03-06 02:32 出处:网络
What is the meaning of that exception ava.lang.IllegalStateException: ViewStub must have a non-null ViewGroup viewParent

What is the meaning of that exception

ava.lang.IllegalStateException: ViewStub must have a non-null ViewGroup viewParent

i am creating an array of View Stub and adding them to a linearLayout but this exception is showing at run time

for(int i=0;i<1;i++)
{
  try
     {
       stub[0]=new ViewStub(getApplicationContext(),R.layout.view_stub_layout);

      //Viewv=stub[i].inflate(getApplicationContext(),R.layout.view_stub_layout,mainLayout);
      //stub[0].setLayoutResource(R.layout.view_stub_layout);
    开发者_JS百科  View v;
      v=stub[0].inflate();
      mainLayout.addView(v);
      v=null;
  }
  catch(Exception e){
      e.getMessage();
  }
}


Your ViewStub don't have a parent, that's why you catch Exception. You must add ViewStub in Layout at first, after you can inflate it to another View.

Why you use ViewStub? Do you really need it? Maybe it can be good solution:

try {
    View.inflate(getApplicationContext(), R.layout.view_stub_layout, mainLayout);
} catch(Exception e){
    e.getMessage();
}

If you need to keep added Views:

try {
    views[i] = View.inflate(getApplicationContext(), R.layout.view_stub_layout, null);
    mainLayout.add(views[i]);
} catch(Exception e){
    e.getMessage();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消