如何为线性布局元素设置setLayoutParams

时间:2011-04-19 11:27:08

标签: android android-layout

如何为setLayoutParams()元素设置LinearLayout。在MainActivity.java。我为set layout params编写了以下代码,并在第50行显示了nullPointerException,最后我粘贴了Logcat文件。

layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 50);
imageview1.setLayoutParams(layoutParams); // line no 50

layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 50);
textview1.setLayoutParams(layoutParams);

layoutParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
listview01.setLayoutParams(layoutParams);

这是我的xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stackFromBottom="true"
>
<ImageView
android:id="@+id/imageview1"
android:layout_width="wrap_content"
android:layout_height="50px"
android:background="@drawable/applicationbar"
android:layout_x="0px"
android:layout_y="0px"
>
</ImageView>
<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="50px"
android:text="TextView"
android:layout_x="0px"
android:layout_y="55px"
>
</TextView>
<ListView android:id="@+id/ListView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10px" 
android:layout_marginRight="10px"
android:layout_marginTop="35px" 
android:layout_marginBottom="40px"
android:paddingLeft="0px"
android:paddingRight="0px" />
</LinearLayout>

Log cat:

04-19 16:47:22.221: ERROR/AndroidRuntime(3437): Uncaught handler: thread main exiting due to uncaught exception
04-19 16:47:22.221: ERROR/AndroidRuntime(3437): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android/com.android.Listview}: java.lang.NullPointerException
04-19 16:47:22.221: ERROR/AndroidRuntime(3437):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
04-19 16:47:22.221: ERROR/AndroidRuntime(3437):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
04-19 16:47:22.221: ERROR/AndroidRuntime(3437):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
04-19 16:47:22.221: ERROR/AndroidRuntime(3437):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
04-19 16:47:22.221: ERROR/AndroidRuntime(3437):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-19 16:47:22.221: ERROR/AndroidRuntime(3437):     at android.os.Looper.loop(Looper.java:123)
04-19 16:47:22.221: ERROR/AndroidRuntime(3437):     at android.app.ActivityThread.main(ActivityThread.java:4363)
04-19 16:47:22.221: ERROR/AndroidRuntime(3437):     at java.lang.reflect.Method.invokeNative(Native Method)
04-19 16:47:22.221: ERROR/AndroidRuntime(3437):     at java.lang.reflect.Method.invoke(Method.java:521)
04-19 16:47:22.221: ERROR/AndroidRuntime(3437):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-19 16:47:22.221: ERROR/AndroidRuntime(3437):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-19 16:47:22.221: ERROR/AndroidRuntime(3437):     at dalvik.system.NativeStart.main(Native Method)
04-19 16:47:22.221: ERROR/AndroidRuntime(3437): Caused by: java.lang.NullPointerException
04-19 16:47:22.221: ERROR/AndroidRuntime(3437):     at com.android.Listview.onCreate(Listview.java:40)
04-19 16:47:22.221: ERROR/AndroidRuntime(3437):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-19 16:47:22.221: ERROR/AndroidRuntime(3437):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)

2 个答案:

答案 0 :(得分:29)

imageview1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 50));  
textview1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 50));
listview01.setLayoutParams(new
       LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));

答案 1 :(得分:6)

在活动开始时onCreate尝试此操作(尽管应该替换照片)。不要忘记评论默认情况下出现的setContentView

// setContentView(R.layout.activity_main);

TextView label = new TextView(this);
label.setText("This is working, congrats!");
label.setTextSize(20);
label.setGravity(Gravity.CENTER_HORIZONTAL);

LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setLayoutParams(new WindowManager.LayoutParams());

ll.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
ll.setGravity(Gravity.CENTER);
ll.addView(label);
setContentView(ll);