基本上,我创建了一个单独的xml,其中包含一个线性布局,其中包含一个图像和一个textview,我需要在另一个线性布局中使用这个线性布局,我需要将我的第一个线性布局动态添加到第二个线性布局中。
第一个线性布局,一个单独的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>
android studio代码:
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)
有人能帮我一下吗,会很有帮助的。
1条答案
按热度按时间c3frrgcw1#
其中一个布局不能作为视图存在。如果你的活动或片段是用
settingButtonHolder
布局然后你需要使用一个LayoutInflater
.一旦你使用了
LayoutInflater
您将收到一个视图示例,您可以使用该示例将其添加到您的线性布局中linearLayout2
. 直到你膨胀了你的新布局,你会一直看到NPE,因为该布局中的视图将不存在。您必须使用膨胀视图示例(linearLayout2
)访问视图,最好通过linearLayout2.findViewById(R.id.X)
.试试这个,告诉我它是否对你有帮助,帕诺斯。