窗口会话崩溃

时间:2011-09-20 14:00:10

标签: android android-layout android-windowmanager

我有以下代码在活动顶部添加View并在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并不是最重要的,我甚至没有在显示器上看到它。 其次,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)

1 个答案:

答案 0 :(得分:0)

所以你可以这样做:

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,则需要使用relativeLayout并将imageView与attribut“layout_alignParentTop”放在一起。

希望会有所帮助。

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