简体   繁体   中英

How can I dynamically add Views that use XML Layouts to the ContentView?

Within my main Activity, I create a FrameLayout with child Views and set it as the ContentView. Now, staying within the same Activity, after onCreate() has executed fully, I need to be able to add and remove child Views from the FrameLayout, dynamically. I achieved this by using a Handler to pause the main Thread and then add or removed the child Views as needed.

This all works great when I am dealing with Views that I programmatically create, but when I attempt to do the same with a View that uses a XML layout, I get a Null Pointer Exception from the ViewGroup that I am trying to add to the FrameLayout.

Are there specific steps I can use to achieve this without the use of Fragments?

I can elaborate more if some don't understand what I am asking. I could provide code chunks if it would help but I think most will get the gist of what I am doing from my short explanation. I am just looking for someone to point me into the right direction.

Here is the LogCat chunk for the exception taht was thrown:

11-15 13:40:42.977: E/AndroidRuntime(14956): FATAL EXCEPTION: main
11-15 13:40:42.977: E/AndroidRuntime(14956): java.lang.NullPointerException
11-15 13:40:42.977: E/AndroidRuntime(14956):    at android.view.ViewGroup.addView(ViewGroup.java:1821)
11-15 13:40:42.977: E/AndroidRuntime(14956):    at android.view.ViewGroup.addView(ViewGroup.java:1808)
11-15 13:40:42.977: E/AndroidRuntime(14956):    at com.beastcodes.framework.impl.AndroidGame.addScreenFragment(AndroidGame.java:207)
11-15 13:40:42.977: E/AndroidRuntime(14956):    at com.beastcodes.framework.impl.AndroidGame.addAllScreenFragments(AndroidGame.java:213)
11-15 13:40:42.977: E/AndroidRuntime(14956):    at com.beastcodes.framework.impl.AndroidGame.access$2(AndroidGame.java:211)
11-15 13:40:42.977: E/AndroidRuntime(14956):    at com.beastcodes.framework.impl.AndroidGame$3.run(AndroidGame.java:61)
11-15 13:40:42.977: E/AndroidRuntime(14956):    at android.os.Handler.handleCallback(Handler.java:587)
11-15 13:40:42.977: E/AndroidRuntime(14956):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-15 13:40:42.977: E/AndroidRuntime(14956):    at android.os.Looper.loop(Looper.java:130)
11-15 13:40:42.977: E/AndroidRuntime(14956):    at android.app.ActivityThread.main(ActivityThread.java:3691)
11-15 13:40:42.977: E/AndroidRuntime(14956):    at java.lang.reflect.Method.invokeNative(Native Method)
11-15 13:40:42.977: E/AndroidRuntime(14956):    at java.lang.reflect.Method.invoke(Method.java:507)
11-15 13:40:42.977: E/AndroidRuntime(14956):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
11-15 13:40:42.977: E/AndroidRuntime(14956):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
11-15 13:40:42.977: E/AndroidRuntime(14956):    at dalvik.system.NativeStart.main(Native Method)

您必须先使用LayoutInflater从XML扩展视图,然后再将其添加到FrameLayout。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM