java - 将 XML 中的线性布局动态添加到另一个线性布局
问题描述
基本上,我创建了一个单独的 XML,其中包含一个包含图像和文本视图的线性布局,并且我需要在另一个线性布局内使用这个线性布局,我需要将我的第一个线性布局动态添加到第二个线性布局。
第一个线性布局,一个单独的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/storeLocation"
android:layout_width="@dimen/settings_item_width"
android:layout_height="@dimen/settings_item_height"
android:layout_marginBottom="10dp"
android:visibility="@integer/sales_order_settings_visibility"
style="@style/dashboard_btn"
android:onClick="onStoreLocationClick"
android:clickable="true"
android:orientation="horizontal">
<LinearLayout
android:layout_width="@dimen/settings_item_image_container_width"
android:layout_height="match_parent"
android:gravity="center">
<ImageView
android:layout_width="@dimen/setting_item_image_size"
android:layout_height="@dimen/setting_item_image_size"
android:tint="@color/cherry_red_without_opacity"
android:src="@drawable/ic_location_icon" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingRight="40dp"
android:gravity="center|left">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Store Location"
android:textColor="@color/white"
android:textSize="@dimen/top_section_purchase_id_font" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
第二个线性布局,单独的 XML:
<LinearLayout
android:id="@+id/settingButtonHolder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
</LinearLayout>
安卓工作室代码:
linearLayout = (LinearLayout) findViewById(R.id.settingButtonHolder);
linearLayout2 = (LinearLayout) findViewById(R.id.storeLocation);
linearLayout.addView(linearLayout2);
这不起作用,我得到一个空指针异常:
12-16 16:48:52.402 21571-21571/io.apptizer.business.clover E/AndroidRuntime: FATAL EXCEPTION: main
Process: io.apptizer.business.clover, PID: 21571
java.lang.RuntimeException: Unable to start activity ComponentInfo{io.apptizer.business.clover/io.apptizer.pos.activity.ApplicationSettingsActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.view.ViewGroup.addView(ViewGroup.java:3353)
at android.view.ViewGroup.addView(ViewGroup.java:3336)
at io.apptizer.pos.activity.applicationSetting.ApplicationSettingsActivityCommon.onCreate(ApplicationSettingsActivityCommon.java:151)
at io.apptizer.pos.activity.ApplicationSettingsActivity.onCreate(ApplicationSettingsActivity.java:78)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
我可以尝试什么来解决这个问题?
解决方案
其中一个布局不得作为视图存在。如果您的活动或片段是使用settingButtonHolder
布局构建的,那么您需要使用LayoutInflater
.
一旦您使用了 ,LayoutInflater
您将收到一个视图实例,您可以使用它来添加到您的线性布局中以代替linearLayout2
. 在您扩充新布局之前,您将继续看到 NPE,因为该布局中的视图将不存在。您将不得不使用膨胀视图实例 ( linearLayout2
) 来访问视图,最好是通过linearLayout2.findViewById(R.id.X)
.
试试这个,让我知道它是否对你有帮助,帕诺斯。
推荐阅读
- sparql - Sparql DBPedia 不返回 foaf:name 查询
- javascript - 使用请求库的 Firebase 函数未触发
- tensorflow - SSD-shufflenet-V2-FPN 比 Mobilenet V2 慢
- php - 从 URL 加载下一组结果 - PHP cURL
- webpack - 使用 Haul 时是否可以覆盖 AssetResolver 测试表达式?
- powerbi - 返回由另一列排序的最高值
- c++ - C ++将字符串数组的元素存储到变量中
- c++ - 反转后不返回最后一个元素
- android-studio - Android Studio 3.2 无法解析布局中的符号
- game-maker - 游戏制作者:玩家移动期间未调用鼠标按下事件