2011-02-11 186 views
1

視圖我用下面的代碼在佈局通過創建Java代碼

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 

是否有任何其他好的方式動態地添加視圖到線性佈局?我在這裏做錯了什麼?

回答

5

我猜測認爲R.layout.row在一個單獨的XML文件中定義的,並且要包括在這裏。如果是這樣的話,

findViewById(R.layout.row)

不會起作用,因爲R.layout.row不在當前活動可見。

您需要使用佈局充氣器來充氣視圖,然後才能將其添加到線性佈局。這樣做的方法是

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)); 
+0

非常感謝你現在的工作 – Jithin 2011-02-11 05:13:39

1

您給addView的電話是錯誤的。

要調用addView(View, int),但它應該是:addView(View, LayoutParams)

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