inflate a button using xml to style

ぐ巨炮叔叔 提交于 2019-12-11 04:24:16

问题


Trying to inflate a Button

code:

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>

exception (stack trace):

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

回答1:


I was having the same issue. Take a look at my question regarding adding an object from the style xml using the inflater.

The answer involved the following code:

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



回答2:


Are you trying to add the button to another view? The exception seems to be saying that.
Try get rid of the LinearLayout that wrapped the button.



来源:https://stackoverflow.com/questions/6904900/inflate-a-button-using-xml-to-style

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!