我需要在服务中添加两个浮动视图。 SurfaceView和RelativeLayout。如果仅添加曲面,则可以,但是如果添加相对布局,则会发生崩溃。这是错误日志。
02-20 05:52:49.717 442-809 / system_process A / WindowManagerService:窗口会话崩溃 java.lang.NullPointerException:尝试从null对象引用上的字段'int android.view.ViewGroup $ LayoutParams.width'中读取 在com.android.server.wm.WindowManagerService.relayoutWindow(WindowManagerService.java:3898) 在com.android.server.wm.Session.relayout(Session.java:207) 在android.view.IWindowSession $ Stub.onTransact(IWindowSession.java:273) 在com.android.server.wm.Session.onTransact(Session.java:140) 在android.os.Binder.execTransact(Binder.java:446) 02-20 05:52:49.718 10777-10777 / com.zmsk.zface D / AndroidRuntime:关闭VM
--------- beginning of crash
02-20 05:52:49.720 10777-10777 / com.zmsk.zface E / Android运行时:致命异常:主要 流程:com.zmsk.zface,PID:10777 java.lang.NullPointerException:尝试从null对象引用上的字段'int android.view.ViewGroup $ LayoutParams.width'中读取 在android.os.Parcel.readException(Parcel.java:1552) 在android.os.Parcel.readException(Parcel.java:1499) 在android.view.IWindowSession $ Stub $ Proxy.relayout(IWindowSession.java:1109) 在android.view.ViewRootImpl.relayoutWindow(ViewRootImpl.java:5581) 在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1623) 在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1161) 在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6198) 在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:767) 在android.view.Choreographer.doCallbacks(Choreographer.java:580) 在android.view.Choreographer.doFrame(Choreographer.java:550) 在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:753) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:135) 在android.app.ActivityThread.main(ActivityThread.java:5280) 在java.lang.reflect.Method.invoke(本机方法) 在java.lang.reflect.Method.invoke(Method.java:372) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:963) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:758)
让我们看看代码: 这是处理程序,我在收到MSG_SHOW_PREVIEW_VIEW
之后创建了浮动视图 private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
long beginTime = SystemClock.currentThreadTimeMillis();
super.handleMessage(msg);
Log.d(TAG, "ZMFaceService handler what:" + msg.what);
if (msg.what == MSG_UPDATE_FACE_DB) {
new UpdateFaceDBTask().execute();
} else if (msg.what == MSG_SHOW_PREVIEW_VIEW) {
if (mPreviewSurface != null && mPreviewSurface.isAttachedToWindow()) {
Log.d(TAG, "MSG_SHOW_PREVIEW_VIEW mPreviewSurface existed, ignore.");
} else {
showFloatBackgroundView();
showFloatPreviewView();
}
} else if (msg.what == MSG_HIDE_PREVIEW_VIEW) {
removePreviewWindow();
}
Log.d(TAG, "ZMFaceService handler time:" + (SystemClock.currentThreadTimeMillis() - beginTime));
}
};
创建RelativeLayout代码(导致崩溃的位置):
private void showFloatBackgroundView () {
Log.d(TAG, "showFloatBackgroundView");
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams();
wmParams.packageName = this.getPackageName();
wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_SCALED
| WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
wmParams.type = WindowManager.LayoutParams.TYPE_PHONE;
wmParams.format = PixelFormat.RGBA_8888;
wmParams.gravity = Gravity.START | Gravity.TOP;
Log.d(TAG, "showFloatBackgroundView:Width:" + width + " height:" + height);
wmParams.width = 666;
wmParams.height = 1080;
wmParams.x = 1254;
wmParams.y = 0;
mFloatBackground = new RelativeLayout(this);
mFloatBackground.setBackgroundResource(R.color.background_color);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(666, 1080);
mFloatBackground.setLayoutParams(params);
windowManager.addView(mFloatBackground, wmParams);
mTitleView = new TextView(this);
mTitleView = new TextView(this);
mTitleView.setText(R.string.app_title);
mTitleView.setTextColor(getResources().getColor(R.color.main_text_color));
mTitleView.setTextSize(60);
RelativeLayout.LayoutParams titleParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
titleParams.topMargin = 40;
titleParams.leftMargin = 53;
mFloatBackground.addView(mTitleView, titleParams);
mComparedAvatar = new ImageView(this);
mComparedAvatar.setBackgroundResource(R.drawable.zm_face_background_hori);
RelativeLayout.LayoutParams comparedAvatarParam = new RelativeLayout.LayoutParams(170, 150);
comparedAvatarParam.leftMargin = 140;
comparedAvatarParam.topMargin = 675;
mFloatBackground.addView(mComparedAvatar, comparedAvatarParam);
mDetectedAvatar = new ImageView(this);
mDetectedAvatar.setBackgroundResource(R.drawable.zm_face_background_hori);
RelativeLayout.LayoutParams detectedAvatarParam = new RelativeLayout.LayoutParams(170, 150);
detectedAvatarParam.leftMargin = 356;
detectedAvatarParam.topMargin = 675;
mFloatBackground.addView(mDetectedAvatar, detectedAvatarParam);
ImageView compareContainerBg = new ImageView(this);
compareContainerBg.setBackgroundResource(R.drawable.zm_capture_preview_hori);
RelativeLayout.LayoutParams faceContainerBgParams = new RelativeLayout.LayoutParams(526, 350);
faceContainerBgParams.leftMargin = 70;
faceContainerBgParams.topMargin = 620;
mFloatBackground.addView(compareContainerBg, faceContainerBgParams);
mResultText = new TextView(this);
mResultText.setText(R.string.id_flash_tips);
mResultText.setTextSize(30);
mResultText.setTextColor(getResources().getColor(R.color.main_text_color));
RelativeLayout.LayoutParams resultParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
int titleLeftMargin = 173;
resultParams.topMargin = 875;
resultParams.leftMargin = titleLeftMargin;
mFloatBackground.addView(mResultText, resultParams);
Log.d(TAG, "showFloatBackgroundView end");
}
创建表面浮动视图代码:
private void showFloatPreviewView () {
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams();
wmParams.packageName = this.getPackageName();
wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_SCALED
| WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
wmParams.type = WindowManager.LayoutParams.TYPE_PHONE;
wmParams.format = PixelFormat.RGBA_8888;
wmParams.gravity = Gravity.START | Gravity.TOP;
wmParams.width = 500;
wmParams.height = 375;
wmParams.x = x + 83;
wmParams.y = y + 200;
mPreviewSurface = new ZMPreviewSurface(this);
mPreviewSurface.setResultListener(this);
mFaceCanvasView = mPreviewSurface.getFaceCanvasView();
windowManager.addView(mPreviewSurface, wmParams);
}
这真的很奇怪。如果我将View替换为RelativeLayout,崩溃也会发生。有人建议吗?非常感谢!