1
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
4

2 回答 2

1

根据文档,如果此视图是嵌入式子视图,则 getParent() 返回父视图。

视图是嵌入的孩子吗?如果不是,getParent() 将返回 null。

于 2013-09-30T13:23:59.517 回答
1

它与Android版本无关,而是与不同的布局文件夹有关。

检查所有 xml 变体文件的 ID。

您通过在 java 代码中调用 R.id.* 使用的所有 id 都应该在同一个 xml 文件的所有变体中定义。

于 2013-09-30T14:15:21.967 回答