0

I am trying to call LinearLayout by its id. When I am trying to do so I am getting NoSuchFieldError.

LinearLayout l1 = (LinearLayout)findViewById(R.id.content2);
setContentView(l1);
1
  • 3
    findViewById() is for getting l1 after you've already set the content view from an XML resource. This sample code makes no sense... Commented Mar 22, 2012 at 18:53

2 Answers 2

1

The way you are using is not correct.

setContentView(R.layout.main) must set with any layout say main.xml for your case.

and now the main layout is having the LinearLayout with id content2.

Also if you want to use setContentView directly create a dynamic linear layout i.e not in xml.

Linearlayout l1 = new LinearLayout(this);
//Set Layout params and bla bla... as per your need..
now setContentView(l1);
Sign up to request clarification or add additional context in comments.

Comments

0

You can find that id, if and only if you have given that id in the xml resource file which you are inflating. Without loading the xml resource file you cannot find the id. YOu can load the xml resource file using the setContentView(R.layout.main); in the Activity onCreate(). The code for finding id will be like LinearLayout l = findViewById(R.id.content);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.