通过Java代码创建视图

时间:2011-02-11 04:30:18

标签: android

我使用以下代码在布局中添加视图

LinearLayout linearLayout_row1_recent_released = (LinearLayout)findViewById(R.id.linearLayout);
    View myview = findViewById(R.layout.row);
     Button button = myview.findViewById(R.id.mybutton);
     linearLayout_row1_recent_released.removeAllViews();
     linearLayout_row1_recent_released.addView(myview,LinearLayout.LayoutParams.FILL_PARENT);

使用row.xml我试图获得一个视图。 我得到的是一个致命的例外。

02-11 02:59:04.278: ERROR/AndroidRuntime(780): FATAL EXCEPTION: main
02-11 02:59:04.278: ERROR/AndroidRuntime(780): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.aaaaa.ap/com.aaaaaa.ap.AppPlaceMain}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.aaaaaa.ap/com.aaaaaa.ap.Store}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.aaa.ap/com.aaaaaa.ap.catalog.Home}: java.lang.NullPointerException
02-11 02:59:04.278: ERROR/AndroidRuntime(780):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1701)
02-11 02:59:04.278: ERROR/AndroidRuntime(780):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1717)
02-11 02:59:04.278: ERROR/AndroidRuntime(780):     at android.app.ActivityThread.access$1500(ActivityThread.java:123)
02-11 02:59:04.278: ERROR/AndroidRuntime(780):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:984)
02-11 02:59:04.278: ERROR/AndroidRuntime(780):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-11 02:59:04.278: ERROR/AndroidRuntime(780):     at android.os.Looper.loop(Looper.java:126)
02-11 02:59:04.278: ERROR/AndroidRuntime(780):     at android.app.ActivityThread.main(ActivityThread.java:3900)
02-11 02:59:04.278: ERROR/AndroidRuntime(780):     at java.lang.reflect.Method.invokeNative(Native Method)
02-11 02:59:04.278: ERROR/AndroidRuntime(780):     at java.lang.reflect.Method.invoke(Method.java:491)
02-11 02:59:04.278: ERROR/AndroidRuntime(780):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
02-11 02:59:04.278: ERROR/AndroidRuntime(780):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
02-11 02:59:04.278: ERROR/AndroidRuntime(780):     at dalvik.system.NativeStart.main(Native Method)
02-11 02:59:04.278: ERROR/AndroidRuntime(780): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.aaaaaa.ap/com.aaaaaa.ap.Store}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.aaaaaaa.ap/com.aaaaaaa.ap.catalog.Home}: java.lang.NullPointerException
02-11 02:59:04.278: ERROR/AndroidRuntime(780):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1701)
02-11 02:59:04.278: ERROR/AndroidRuntime(780):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:1543)
02-11 02:59:04.278: ERROR/AndroidRuntime(780):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:130)
02-11 02:59:04.278: ERROR/AndroidRuntime(780):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:342)
02-11 02:59:04.278: ERROR/AndroidRuntime(780):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:680)
02-11 02:59:04.278: ERROR/AndroidRuntime(780):     at android.widget.TabHost.setCurrentTab(TabHost.java:349)
02-11 02:59:04.278: ERROR/AndroidRuntime(780):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
02-11 02:59:04.278: ERROR/AndroidRuntime(780):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1665)
02-11 02:59:04.278: ERROR/AndroidRuntime(780):     ... 11 more
02-11 02:59:04.278: ERROR/AndroidRuntime(780): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.aaaaaaa.ap/com.aaaaaa.ap.catalog.Home}: java.lang.NullPointerException
02-11 02:59:04.278: ERROR/AndroidRuntime(780):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1701)
02-11 02:59:04.278: ERROR/AndroidRuntime(780):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:1543)
02-11 02:59:04.278: ERROR/AndroidRuntime(780):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:130)
02-11 02:59:04.278: ERROR/AndroidRuntime(780):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:342)
02-11 02:59:04.278: ERROR/AndroidRuntime(780):     at com.aaaaad.ap.Store.onCreate(Store.java:29)
02-11 02:59:04.278: ERROR/AndroidRuntime(780):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
02-11 02:59:04.278: ERROR/AndroidRuntime(780):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1665)
02-11 02:59:04.278: ERROR/AndroidRuntime(780):     ... 20 more
02-11 02:59:04.278: ERROR/AndroidRuntime(780): Caused by: java.lang.NullPointerException
02-11 02:59:04.278: ERROR/AndroidRuntime(780):     at android.view.ViewGroup.addView(ViewGroup.java:2684)
02-11 02:59:04.278: ERROR/AndroidRuntime(780):     at com.aaaaa.ap.catalog.Home.onCreate(Home.java:51)
02-11 02:59:04.278: ERROR/AndroidRuntime(780):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
02-11 02:59:04.278: ERROR/AndroidRuntime(780):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1665)
02-11 02:59:04.278: ERROR/AndroidRuntime(780):     ... 26 more

有没有其他好方法可以动态地将视图添加到线性布局?我在这做错了什么?

2 个答案:

答案 0 :(得分:5)

我猜测视图R.layout.row是在一个单独的XML文件中定义的,并且您希望将其包含在此处。如果是这样的话,

findViewById(R.layout.row)

将无效,因为R.layout.row在当前活动中不可见。

您需要使用布局inflater来扩大视图,然后您可以将其添加到线性布局。这样做的方法是

LayoutInflaterinflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View myView = inflater.inflate(R.layout.row, null);
linearLayout_row1_recent_released.addView(myview,
                       new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT) );

答案 1 :(得分:1)

您对addView的来电是错误的。

您正在呼叫addView(View, int),但它应该是:addView(View, LayoutParams)

linearLayout_row1_recent_released.addView(myview,
    new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 
                                  LayoutParams.WRAP_CONTENT) );