0
我有以下代碼在活動頂部添加一個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)
我沒有佈局的參考。 –
您的活動沒有xml關聯關係嗎? 如果沒有,您可以替換: 'setContentView(R.layout.main);最終LinearLayout linearLayout =(LinearLayout)findViewById(R.id.id_mainlinearlayout);' 由此: '最終LinearLayout linearLayout = new LinearLayout(this); \t \t linearLayout.setLayoutParams(新WindowManager.LayoutParams( \t \t \t \t LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); \t \t setContentView(linearLayout);' – grattmandu03
ContentView設置在超類中。 –