Мне нужно добавить два вида с плавающей точкой в сервис.SurfaceView и RelativeLayout.Если я только добавлю поверхность, это нормально, но если я добавлю относительный макет, произойдет сбой.Вот журнал ошибок.
02-20 05: 52: 49.717 442-809 / system_process A / WindowManagerService: сбой сеанса окна java.lang.NullPointerException: попытка чтения из поля 'int android.view.ViewGroup$ LayoutParams.width 'для пустой ссылки на объект на com.android.server.wm.WindowManagerService.relayoutWindow (WindowManagerService.java:3898) на com.android.server.wm.Session.relayout (Session.java:207) на андроиде.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/ AndroidRuntime: ИСКЛЮЧИТЕЛЬНОЕ ИСКЛЮЧЕНИЕ: основной процесс: com.zmsk.zface, PID: 10777 java.lang.NullPointerException: попытка чтения из поля '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)..view.Choreographer $ CallbackRecord.run (Choreographer.java:767) на android.view.Choreographer.doCallbacks (Choreographer.java:580) на android.view.Choreographer.doFrame (Choreographer.java:550) на андроид.Хореограф $ 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);
}
Это действительно странно.если я заменю RelativeLayout на View, также произошел сбой.У кого-нибудь есть совет?Большое спасибо!