0

Hi i'm building app similar to Chathead. My service is checking if other app is in foregnound and if yes service hide chathead(ImageView move out of linearlayout and linarlayout is untouchable). Problem is, that after few hours running app, when service is checking which app is in foregnound and after want to hide chathead, layoutParams of Chatchead returning NullPointerException. Forexample app stop working when you wake up mobile after night.

Setting 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);

Layout of 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 retuning 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)

My opinion is that this problem cause Android. Bescause app works fine and problem starts after few hours. Any idea to solve this problem?

Lalson
  • 293
  • 1
  • 3
  • 14
  • "My opinion is that this problem cause Android. Bescause app works fine and problem starts after few hours." -- Uhm, no, if that was true, then *all* apps would work and then fail after a few hours. It's definitely *your* code that is breaking. It's impossible to help you though, because you successfully omitted *all* useful information. – 323go Sep 27 '14 at 16:18
  • I add logcat to my post, anything else should i add? – Lalson Sep 27 '14 at 16:25
  • Where do you assign `mMainIconLayoutParams`? Clearly, it's being garbage collected. – 323go Sep 27 '14 at 16:58
  • I assing mMainLayoutParams in methond onCreate()... So should i assing it in onCommandStart? – Lalson Sep 27 '14 at 17:12
  • `onCommandStart()`?? Are you operating in a `Service`?? If so, then there are no `LayoutParams`. If you're accessing an activity from the service, it's no wonder it was garbage collected. – 323go Sep 27 '14 at 20:30
  • I'm sorry. I didn't explain what is chathead. I updated my post. It 's like facebook messenger floating chatheads. So `mainIconlinearlayout` is floating widget. And layoutparams belongs to `mainIconlinearlayout`. I also think that it is garbage collected as i said("My opinion is that this problem cause Android"). But i didn t said in right way. So is there any idea how to stop garbadge collect it? – Lalson Sep 28 '14 at 08:36

0 Answers0