我正在尝试在Fragments之间交换,其中一个答案是:你无法删除在XML文件中静态构建的片段,我创建了一个容器XML文件并使用setContentView()将容器设置为第一个UI, 这是我的容器文件(称为conrainer_new.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/second_screen"
android:name="com.eiq.randomsendriod.Secund_screen"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<FrameLayout
android:id="@+id/conver_list_frag"
android:name="com.eiq.randomsendriod.ConverList"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<FrameLayout
android:id="@+id/mainpage"
android:name="com.eiq.randomsendriod.mainpage"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<FrameLayout
android:id="@+id/massenger"
android:name="com.eiq.randomsendriod.Massenger_frag.java"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
并在onCreate方法中执行:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.container_new);
}
结果: 应用程序崩溃,在LogCat屏幕中我得到:
11-22 16:10:11.698: E/AndroidRuntime(1184): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.eiq.randomsendriod/com.eiq.randomsendriod.SendRandom}: java.lang.NullPointerException
11-22 16:10:11.698: E/AndroidRuntime(1184): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
11-22 16:10:11.698: E/AndroidRuntime(1184): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
11-22 16:10:11.698: E/AndroidRuntime(1184): at android.app.ActivityThread.access$700(ActivityThread.java:135)
11-22 16:10:11.698: E/AndroidRuntime(1184): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
11-22 16:10:11.698: E/AndroidRuntime(1184): at android.os.Handler.dispatchMessage(Handler.java:102)
11-22 16:10:11.698: E/AndroidRuntime(1184): at android.os.Looper.loop(Looper.java:137)
11-22 16:10:11.698: E/AndroidRuntime(1184): at android.app.ActivityThread.main(ActivityThread.java:4998)
11-22 16:10:11.698: E/AndroidRuntime(1184): at java.lang.reflect.Method.invokeNative(Native Method)
11-22 16:10:11.698: E/AndroidRuntime(1184): at java.lang.reflect.Method.invoke(Method.java:515)
11-22 16:10:11.698: E/AndroidRuntime(1184): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-22 16:10:11.698: E/AndroidRuntime(1184): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
11-22 16:10:11.698: E/AndroidRuntime(1184): at dalvik.system.NativeStart.main(Native Method)
11-22 16:10:11.698: E/AndroidRuntime(1184): Caused by: java.lang.NullPointerException
11-22 16:10:11.698: E/AndroidRuntime(1184): at com.eiq.randomsendriod.SendRandom.onCreate(SendRandom.java:82)
11-22 16:10:11.698: E/AndroidRuntime(1184): at android.app.Activity.performCreate(Activity.java:5243)
11-22 16:10:11.698: E/AndroidRuntime(1184): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-22 16:10:11.698: E/AndroidRuntime(1184): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
11-22 16:10:11.698: E/AndroidRuntime(1184): ... 11 more
通过将上述XML文件更改为:
来解决 <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
并删除所有其他代码!