Discussion:
Programmatically Adding a LinearLayout Error
csyperski
2009-07-28 02:38:14 UTC
Permalink
I am trying to add a LinearLayout using only code to a Activity and I
keep getting as error. Here is a proof of concept piece of code:

ackage org.syperiorsoft;



import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class ViewTest extends Activity {

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

LinearLayout llRoot = (LinearLayout)findViewById(R.id.someid);
if ( llRoot != null )
{
LinearLayout llDetails = new LinearLayout(this);
llDetails.setLayoutParams(new LayoutParams
(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
llDetails.setGravity(Gravity.LEFT);

ImageView imEdit = new ImageView(this);
imEdit.setImageResource(R.drawable.edit);
imEdit.setLayoutParams(new LayoutParams(32, 32));

LinearLayout llVert = new LinearLayout(this);
llVert.setOrientation(LinearLayout.VERTICAL);
llVert.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));

llDetails.addView(imEdit);
llDetails.addView(llVert);


llRoot.addView(llDetails);
}
}
}



Here is the stack:

Thread [<3> main] (Suspended (exception RuntimeException))
LinearLayout.getBaseline() line: 151
LinearLayout.measureHorizontal(int, int) line: 644
LinearLayout.onMeasure(int, int) line: 280
LinearLayout(View).measure(int, int) line: 7115
LinearLayout(ViewGroup).measureChildWithMargins(View, int, int, int,
int) line: 2875
LinearLayout.measureChildBeforeLayout(View, int, int, int, int, int)
line: 888
LinearLayout.measureVertical(int, int) line: 350
LinearLayout.onMeasure(int, int) line: 278
LinearLayout(View).measure(int, int) line: 7115
FrameLayout(ViewGroup).measureChildWithMargins(View, int, int, int,
int) line: 2875
FrameLayout.onMeasure(int, int) line: 245
FrameLayout(View).measure(int, int) line: 7115
LinearLayout.measureVertical(int, int) line: 464
LinearLayout.onMeasure(int, int) line: 278
LinearLayout(View).measure(int, int) line: 7115
PhoneWindow$DecorView(ViewGroup).measureChildWithMargins(View, int,
int, int, int) line: 2875
PhoneWindow$DecorView(FrameLayout).onMeasure(int, int) line: 245
PhoneWindow$DecorView(View).measure(int, int) line: 7115
ViewRoot.performTraversals() line: 698
ViewRoot.handleMessage(Message) line: 1482
ViewRoot(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 123
ActivityThread.main(String[]) line: 3948
Method.invokeNative(Object, Object[], Class, Class[], Class, int,
boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 521
ZygoteInit$MethodAndArgsCaller.run() line: 782
ZygoteInit.main(String[]) line: 540
NativeStart.main(String[]) line: not available [native method]


I hope someone can help me out, I know I am missing something here.
Thanks to all in advance!

Thanks
Mark Murphy
2009-07-28 10:43:35 UTC
Permalink
Post by csyperski
I am trying to add a LinearLayout using only code to a Activity and I
keep getting as error.
LinearLayout llVert = new LinearLayout(this);
llVert.setOrientation(LinearLayout.VERTICAL);
llVert.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
llDetails.addView(imEdit);
llDetails.addView(llVert);
llRoot.addView(llDetails);
Note that you do not have any LinearLayout.Params for this addView() call.
That is not a complete stack trace. That is Eclipse telling you it has
suspended execution of your program. In particular, you have lost the
information about that RuntimeException, which would be really useful
for you to figure out what your problem is.
--
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy

Android Development Wiki: http://wiki.andmob.org
Loading...