4
裏面的方法,我稱之爲addView
和removeView
如下:調用的ViewGroup#addView和一個ViewGroup#removeView導致NullPointerException異常
public class EnclosingClass extends FrameLayout {
...
void fooMethod() {
...
viewGroup1.addView(this);
viewGroup2.removeView(this);
...
}
...
}
這部作品肖像模式罰款(它被稱爲很多次都沒有任何問題),但當我改變屏幕爲橫向的方向,我得到了NullPointerException
:(僅供參考,fooMethod
正在從surfaceChanged方向變化時的稱呼):
18376 AndroidRuntime E FATAL EXCEPTION: main
18376 AndroidRuntime E Process: com.google.android.apps.chrome, PID: 18376
18376 AndroidRuntime E java.lang.NullPointerException
18376 AndroidRuntime E at android.widget.FrameLayout.layoutChildren(FrameLayout.java:405)
18376 AndroidRuntime E at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
18376 AndroidRuntime E at android.view.View.layout(View.java:14817)
18376 AndroidRuntime E at android.view.ViewGroup.layout(ViewGroup.java:4631)
18376 AndroidRuntime E at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
18376 AndroidRuntime E at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
18376 AndroidRuntime E at android.view.View.layout(View.java:14817)
18376 AndroidRuntime E at android.view.ViewGroup.layout(ViewGroup.java:4631)
18376 AndroidRuntime E at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
18376 AndroidRuntime E at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
18376 AndroidRuntime E at android.view.View.layout(View.java:14817)
18376 AndroidRuntime E at android.view.ViewGroup.layout(ViewGroup.java:4631)
18376 AndroidRuntime E at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1987)
18376 AndroidRuntime E at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1744)
18376 AndroidRuntime E at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
18376 AndroidRuntime E at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
18376 AndroidRuntime E at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
18376 AndroidRuntime E at android.view.Choreographer.doCallbacks(Choreographer.java:574)
18376 AndroidRuntime E at android.view.Choreographer.doFrame(Choreographer.java:544)
18376 AndroidRuntime E at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
18376 AndroidRuntime E at android.os.Handler.handleCallback(Handler.java:733)
18376 AndroidRuntime E at android.os.Handler.dispatchMessage(Handler.java:95)
18376 AndroidRuntime E at android.os.Looper.loop(Looper.java:136)
18376 AndroidRuntime E at android.app.ActivityThread.main(ActivityThread.java:5017)
18376 AndroidRuntime E at java.lang.reflect.Method.invokeNative(Native Method)
18376 AndroidRuntime E at java.lang.reflect.Method.invoke(Method.java:515)
18376 AndroidRuntime E at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
18376 AndroidRuntime E at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
18376 AndroidRuntime E at dalvik.system.NativeStart.main(Native Method)
在或DER來解決這個問題,我把addView
和removeView
一個Runnable
裏面如下:
void fooMethod() {
...
new Handler().post(new Runnable() {
@Override
public void run() {
viewGroup1.addView(EnclosingClass.this);
viewGroup2.removeView(EnclosingClass.this);
});
...
}
這並不能解決問題,而且似乎一切都工作正常,但我不明白爲什麼。如何將消息隊列中的方法解決問題?另外,爲什麼我在棧跟蹤中看不到任何對我的代碼的引用?我通過逐漸註釋掉代碼來發現問題。