窗口会话崩溃

发布于 2024-12-05 19:14:40 字数 2351 浏览 1 评论 0原文

我有以下代码,在活动之上添加一个视图并在 10 秒后将其删除。

public class MyActivity extends MainActivity {
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);

        final ImageView view = new ImageView(this);
        view.setBackgroundColor(Color.WHITE);

        getWindowManager()
                .addView(
                        view,
                        new WindowManager.LayoutParams(LayoutParams.MATCH_PARENT,
                                LayoutParams.MATCH_PARENT));

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                view.setVisibility(View.GONE);
                getWindowManager().removeView(view);
            }
        }, 10000);

    }
}

但这里发生了两件事: 首先,ImageView 并不是最重要的,我什至没有在 Display 上看到它。 其次,10秒后发生以下错误并且应用程序崩溃

 Window Session Crash
 java.lang.NullPointerException
     at com.android.server.wm.WindowManagerService.relayoutWindow(WindowManagerService.java:2784)
     at com.android.server.wm.Session.relayout(Session.java:157)
     at android.view.IWindowSession$Stub.onTransact(IWindowSession.java:154)
     at com.android.server.wm.Session.onTransact(Session.java:111)
     at android.os.Binder.execTransact(Binder.java:320)
     at dalvik.system.NativeStart.run(Native Method)
    FATAL EXCEPTION: main
    java.lang.NullPointerException
        at android.os.Parcel.readException(Parcel.java:1327)
        at android.os.Parcel.readException(Parcel.java:1275)
        at android.view.IWindowSession$Stub$Proxy.relayout(IWindowSession.java:616)
        at android.view.ViewRoot.relayoutWindow(ViewRoot.java:3104)
        at android.view.ViewRoot.performTraversals(ViewRoot.java:1030)
        at android.view.ViewRoot.handleMessage(ViewRoot.java:2020)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:132)
        at android.app.ActivityThread.main(ActivityThread.java:4028)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:491)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
        at dalvik.system.NativeStart.main(Native Method)

I have the following code that add a View on top of a activity and remove it after 10 seconds.

public class MyActivity extends MainActivity {
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);

        final ImageView view = new ImageView(this);
        view.setBackgroundColor(Color.WHITE);

        getWindowManager()
                .addView(
                        view,
                        new WindowManager.LayoutParams(LayoutParams.MATCH_PARENT,
                                LayoutParams.MATCH_PARENT));

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                view.setVisibility(View.GONE);
                getWindowManager().removeView(view);
            }
        }, 10000);

    }
}

But two things happens here:
First, the ImageView is not on top of all, I even didn't seen it on the Display.
Second, after 10 seconds the following error happens and the application crash

 Window Session Crash
 java.lang.NullPointerException
     at com.android.server.wm.WindowManagerService.relayoutWindow(WindowManagerService.java:2784)
     at com.android.server.wm.Session.relayout(Session.java:157)
     at android.view.IWindowSession$Stub.onTransact(IWindowSession.java:154)
     at com.android.server.wm.Session.onTransact(Session.java:111)
     at android.os.Binder.execTransact(Binder.java:320)
     at dalvik.system.NativeStart.run(Native Method)
    FATAL EXCEPTION: main
    java.lang.NullPointerException
        at android.os.Parcel.readException(Parcel.java:1327)
        at android.os.Parcel.readException(Parcel.java:1275)
        at android.view.IWindowSession$Stub$Proxy.relayout(IWindowSession.java:616)
        at android.view.ViewRoot.relayoutWindow(ViewRoot.java:3104)
        at android.view.ViewRoot.performTraversals(ViewRoot.java:1030)
        at android.view.ViewRoot.handleMessage(ViewRoot.java:2020)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:132)
        at android.app.ActivityThread.main(ActivityThread.java:4028)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:491)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
        at dalvik.system.NativeStart.main(Native Method)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青衫负雪 2024-12-12 19:14:40

所以你可以做这样的事情:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.id_mainlinearlayout);

    // final ImageView view = (ImageView) findViewById(R.id.id_imageview);
    final ImageView view = new ImageView(this);
    view.setBackgroundColor(Color.WHITE);

    linearLayout.addView(view, new WindowManager.LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            view.setVisibility(View.GONE);
            linearLayout.removeView(view);
        }
    }, 10000);

}

并在“main.xml”中的 LinearLayout 上添加一个 id(对我来说:“id_mainlinearlayout”),

这对我来说效果很好。还有另一种解决方案,您可以在 xml 中定义 imageView,然后使用注释行返回。

如果您想将 imageView 添加到 Activity 的顶部,则需要使用relativeLayout 并使用属性“layout_alignParentTop”放置 imageView。

希望会有帮助。

PS:对不起我的英语,我是法国人!

So you can do something like this :

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.id_mainlinearlayout);

    // final ImageView view = (ImageView) findViewById(R.id.id_imageview);
    final ImageView view = new ImageView(this);
    view.setBackgroundColor(Color.WHITE);

    linearLayout.addView(view, new WindowManager.LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            view.setVisibility(View.GONE);
            linearLayout.removeView(view);
        }
    }, 10000);

}

and in adding an id (for me : "id_mainlinearlayout") on your linearLayout in "main.xml"

It works fine for me. There is an other solution where you define your imageView in the xml and you get back with the commented line.

If you want to add your imageView at the top of your activity, you need to use a relativeLayout and place your imageView with the attribut "layout_alignParentTop".

Hope will be helpful.

Ps : sorry for my english, i'm french !!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文