使用xml为按钮充气

时间:2011-08-01 21:48:34

标签: java android layout-inflater

试图给按钮充气

代码:

LinearLayout ll = (LinearLayout)this.getLayoutInflater().inflate(R.layout.assets, null);
Button btn = (Button)ll.getChildAt(0);

R.layout.assets(assets.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:id="@+id/main" 
    android:layout_height="match_parent" 
    android:weightSum="1" 
    android:padding="50px">
    <Button android:text="Button" android:layout_width="wrap_content" android:id="@+id/myButton" android:layout_height="wrap_content"></Button>
</LinearLayout>

异常(堆栈跟踪):

08-01 21:45:48.631: ERROR/AndroidRuntime(12406): FATAL EXCEPTION: main
08-01 21:45:48.631: ERROR/AndroidRuntime(12406): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.foxbusiness.tv/com.foxbusiness.tv.FOXBusinessActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     at android.app.ActivityThread.access$1500(ActivityThread.java:124)
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1016)
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     at android.os.Looper.loop(Looper.java:132)
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     at android.app.ActivityThread.main(ActivityThread.java:4083)
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     at java.lang.reflect.Method.invokeNative(Native Method)
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     at java.lang.reflect.Method.invoke(Method.java:491)
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     at dalvik.system.NativeStart.main(Native Method)
08-01 21:45:48.631: ERROR/AndroidRuntime(12406): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     at android.view.ViewGroup.addViewInner(ViewGroup.java:3145)
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     at android.view.ViewGroup.addView(ViewGroup.java:3034)
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     at android.view.ViewGroup.addView(ViewGroup.java:2991)
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     at android.view.ViewGroup.addView(ViewGroup.java:2971)
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     at com.foxbusiness.tv.FOXBusinessActivity.onCreate(FOXBusinessActivity.java:112)
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1732)
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     ... 11 more

2 个答案:

答案 0 :(得分:5)

我遇到了同样的问题。看看my question regarding adding an object from the style xml using the inflater

答案涉及以下代码:

LinearLayout view = (LinearLayout)LayoutInflater.from(this).inflate(R.layout.my_button, null);
// or LinearLayout buttonView = (LinearLayout)this.getLayoutInflater().inflate(R.layout.my_button, null);
Button myButton = (Button) view.findViewById(R.id.myButton);
view.removeView(myButton);

LinearLayout mainView = (LinearLayout)this.findViewById(R.id.mainLayout);
mainView.addView(myButton);

答案 1 :(得分:1)

您是否尝试将按钮添加到其他视图?例外情况似乎在说。
尝试摆脱包裹按钮的LinearLayout。