private void swapLayouts(final int currentLayoutId, final int requestedLayout) {
View layout = (View) this.findViewById(currentLayoutId);
ViewGroup parent = (ViewGroup) layout.getParent();
int index = parent.indexOfChild(layout);
parent.removeView(layout);
layout = getLayoutInflater().inflate(requestedLayout, parent, false);
parent.addView(layout, index);
}
被NullPointerException
扔在ViewGroup parent = (ViewGroup) layout.getParent();
这是 LogCat 输出:
09-30 19:04:34.465: E/AndroidRuntime(4059): FATAL EXCEPTION: main
09-30 19:04:34.465: E/AndroidRuntime(4059): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.blah.blah/com.blah.blah.VideoActivity}: java.lang.NullPointerException
09-30 19:04:34.465: E/AndroidRuntime(4059): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-30 19:04:34.465: E/AndroidRuntime(4059): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-30 19:04:34.465: E/AndroidRuntime(4059): at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-30 19:04:34.465: E/AndroidRuntime(4059): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-30 19:04:34.465: E/AndroidRuntime(4059): at android.os.Handler.dispatchMessage(Handler.java:99)
09-30 19:04:34.465: E/AndroidRuntime(4059): at android.os.Looper.loop(Looper.java:137)
09-30 19:04:34.465: E/AndroidRuntime(4059): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-30 19:04:34.465: E/AndroidRuntime(4059): at java.lang.reflect.Method.invokeNative(Native Method)
09-30 19:04:34.465: E/AndroidRuntime(4059): at java.lang.reflect.Method.invoke(Method.java:525)
09-30 19:04:34.465: E/AndroidRuntime(4059): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-30 19:04:34.465: E/AndroidRuntime(4059): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-30 19:04:34.465: E/AndroidRuntime(4059): at dalvik.system.NativeStart.main(Native Method)
09-30 19:04:34.465: E/AndroidRuntime(4059): Caused by: java.lang.NullPointerException
09-30 19:04:34.465: E/AndroidRuntime(4059): at com.blah.blah.VideoActivity.swapLayouts(VideoActivity.java:287)
09-30 19:04:34.465: E/AndroidRuntime(4059): at com.blah.blah.VideoActivity.onCreate(VideoActivity.java:75)
09-30 19:04:34.465: E/AndroidRuntime(4059): at android.app.Activity.performCreate(Activity.java:5133)
09-30 19:04:34.465: E/AndroidRuntime(4059): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-30 19:04:34.465: E/AndroidRuntime(4059): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-30 19:04:34.465: E/AndroidRuntime(4059): ... 11 more