gpt4 book ai didi

android - 根据点击使用合并和包含标签动态更改 UI

转载 作者:行者123 更新时间:2023-11-29 14:09:08 24 4
gpt4 key购买 nike

我是 Android 和一般编程的新手,所以放轻松!

我创建了一个 - 可能过于复杂但它做了我想要的 - main.xml 我的布局是这样的:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_scrollview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<LinearLayout
...
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/title"
android:gravity="center"
/>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:stretchColumns="0"
>
<TableRow>
<TextView/>
<Spinner/>
</TableRow>
<TableRow>
<TextView/>
<Spinner/>
</TableRow>
</TableLayout>
<TableLayout
android:id="@+id/main_tablelayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:stretchColumns="*"
>
<include
android:id="@+id/include"
layout="@layout/price0"
/>
<TableRow>
<TextView/>
<EditText/>
</TableRow>
</TableLayout>
</LinearLayout>
</ScrollView>

这是我要包含的选项之一 (price1.xml):

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="@string/price1"
android:gravity="center_vertical"
/>
<EditText
android:id="@+id/price1"
/>
</TableRow>
</merge>

我正在尝试根据其中一个微调器的值动态更改包含,但我在启动应用程序时强制关闭。

logcat 错误是:

01-18 15:31:42.826: ERROR/AndroidRuntime(719): FATAL EXCEPTION: main
01-18 15:31:42.826: ERROR/AndroidRuntime(719): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at android.view.ViewGroup.addViewInner(ViewGroup.java:1976)
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at android.view.ViewGroup.addView(ViewGroup.java:1871)
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at android.widget.TableLayout.addView(TableLayout.java:421)
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at android.view.ViewGroup.addView(ViewGroup.java:1828)
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at android.widget.TableLayout.addView(TableLayout.java:403)
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at android.view.ViewGroup.addView(ViewGroup.java:1808)
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at android.widget.TableLayout.addView(TableLayout.java:394)
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at com.kavi.eta.Calculate$1.onItemSelected(Calculate.java:36)
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at android.widget.AdapterView.fireOnSelected(AdapterView.java:871)
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at android.widget.AdapterView.access$200(AdapterView.java:42)
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:837)
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at android.os.Handler.handleCallback(Handler.java:587)
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at android.os.Handler.dispatchMessage(Handler.java:92)
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at android.os.Looper.loop(Looper.java:123)
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at android.app.ActivityThread.main(ActivityThread.java:3647)
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at java.lang.reflect.Method.invokeNative(Native Method)
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at java.lang.reflect.Method.invoke(Method.java:507)
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-18 15:31:42.826: ERROR/AndroidRuntime(719): at dalvik.system.NativeStart.main(Native Method)

看起来我做错了 java 代码,但我不知道如何正确删除默认包含并将其替换为另一个布局:

public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) { 
int index = formatSpinner.getSelectedItemPosition();
if (index == 0) {

TableLayout tl = (TableLayout)findViewById(R.id.main_tablelayout2);
tl.removeView(findViewById(R.id.include));

LayoutInflater inflater = getLayoutInflater();
tl.addView(inflater.inflate(R.layout.price1, tl, true));
}

很抱歉发了这么长的帖子,但我到处搜索都找不到明确的答案。希望这至少能提供一个像样的例子......

最佳答案

如果您尝试对包含的布局中存在的元素使用 findViewById(),您将得到 NullPointerException。它们不在 setContentView(View) 中添加的布局中,因此您必须在以下代码 fragment 中提供包含标签和 id,例如 includedLayout -

<include android:id="@+id/includedLayout" layout="@layout/yourLayout"/>

并使用普通的 findViewById() 调用找到它

ViewGroup includedLayout = (ViewGroup)findViewById(R.id.includedLayout);

然后使用此对象在包含的布局中查找 View 。

View myVIew = includedLayout.findViewById(R.id.myViewId);

虽然线程是旧的,但我想这个答案将帮助 future 的读者准确地找到他们的布局中发生了什么。

关于android - 根据点击使用合并和包含标签动态更改 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4726008/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com