Chathead LayoutParams NullPointerException

时间:2014-09-27 15:39:08

标签: android nullpointerexception layoutparams

您好我正在构建类似于Chathead的应用。我的服务是检查其他应用程序是否在foregnound中,如果是,则服务隐藏chathead(ImageView移出linearlayout并且linarlayout是不可触摸的)。问题是,在运行应用程序几个小时之后,当服务正在检查哪个应用程序处于foregnound并且在想要隐藏chathead之后,Chatchead的layoutParams返回NullPointerException。例如,当您夜间醒来后,应用程序停止工作。

设置mMainIconLayoutParams

  mMainIconLayoutParams = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR, 
                PixelFormat.TRANSLUCENT);

            mMainIconLayoutParams.gravity = Gravity.TOP | Gravity.LEFT;
            mMainIconLayoutParams.x = 0;
            mMainIconLayoutParams.y = 100;
            mWindowManager.addView(mainIconlinearlayout, mMainIconLayoutParams);

Chathead的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

   <ImageView
      android:id="@+id/main_icon_image"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:clickable="false" />

</LinearLayout>

mMainIconLayoutParams.x重新调整null ...

case ANIMATION_HIDE:
        if (mMainIconLayoutParams.x>(mDisplayWidth/2)) {
            isMainIconStopped = true;
            animationMainIcon = AnimationUtils.loadAnimation(this,
                    R.anim.move_from_center_to_right);
        }
        else
        {.....}

logcat的:

java.lang.NullPointerException
at cz.lala.shortapp.service.Windowservice.moveMainIcon(Windowservice.java:373)
at cz.lala.shortapp.service.Windowservice.access$14(Windowservice.java:355)
at cz.lala.shortapp.service.Windowservice$1$1.run(Windowservice.java:204)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5153)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
at dalvik.system.NativeStart.main(Native Method)

我的观点是这个问题导致Android。因为应用程序工作正常,问题在几个小时后开始。有什么想法解决这个问题吗?

0 个答案:

没有答案