Handler not working

0

I have used the OrientationEventListener which I have coupled with a Handler to update a TextView when the screen rotates:

  MainHandler handler;
TextView tv;
MyOrientationEventListener listener;
class MainHandler extends Handler
{
    @Override
    public void handleMessage(Message msg)
    {
        switch(msg.what)
        {
            case MSG_ORIENTATION:                   
                if(tv!=null)
                    tv.setText(msg.arg1);
                break;
            default:
                if(tv!=null)
                tv.setText("Nothing to see here");
        }
    }
}

This is my OrientationEventListener here:

class MyOrientationEventListener extends OrientationEventListener
{

    public MyOrientationEventListener(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public MyOrientationEventListener(Context context,int attrs)
    {
        super(context,attrs);
    }

    @Override
    public void onOrientationChanged(int orientation) {
        // TODO Auto-generated method stub
        Log.d("MainActivity", "Orientation before rounding: "+orientation);
        orientation=roundRotation(orientation,0);
        Log.d("MainActivity", "Orientation after rounding: "+orientation);
        Message msg=handler.obtainMessage(MSG_ORIENTATION);
        msg.arg1=orientation;
        handler.sendMessage(msg);
    }

    public int getDisplayOrientation()
    {

        int rotation=getWindowManager().getDefaultDisplay().getRotation();
        switch(rotation)
        {
            case Surface.ROTATION_0:
                return 0;
            case Surface.ROTATION_90:
                return 90;
            case Surface.ROTATION_180:
                return 180;
            case Surface.ROTATION_270:
                return 270;
        }
        return 0;
    }

    public int roundRotation(int orientation,int orientationHistory)
    {
        final int ORIENTATION_HYSTERESIS=5;
        boolean changeOrientation=false;
        if(orientationHistory==ORIENTATION_UNKNOWN)
            changeOrientation=true;
        else
        {
            int dist=Math.abs(orientation-orientationHistory);
            dist=Math.min(dist, 360-dist);
            changeOrientation=(dist>=45+ORIENTATION_HYSTERESIS);
        }
        if(changeOrientation)
        {
            int nOrientation=((orientation+45)/90*90)%360;
            Log.d("OrientationEventHandler", "New orientation: "+nOrientation);
            return nOrientation;
        }
        return orientationHistory;
    }

}

I am using the Handler in onCreate to register my Handler to the UI thread,thus it can be used to update the Views on screen.However,this code does not seem to work:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv=(TextView)findViewById(R.id.tv);
    handler=new MainHandler();
    listener=new MyOrientationEventListener(this);
}

I am using listener.enable() in onResume and listener.disable() in onPause methods after invoking their respective superclass methods.When I try to rotate the screen and the rounding occurs:

   MainActivity(9724): Orientation before rounding: 55
   OrientationEventHandler(9724): New orientation: 90
   MainActivity(9724): Orientation after rounding: 90
   ResourceType(9724): No package identifier when getting value for resource number 0x0000005a
   dalvikvm(9724): threadid=1: thread exiting with uncaught exception (group=0x41fdf700)
   AndroidRuntime(9724): FATAL EXCEPTION: main
   AndroidRuntime(9724): android.content.res.Resources$NotFoundException: String resource ID #0x5a
   AndroidRuntime(9724):    at android.content.res.Resources.getText(Resources.java:239)
   AndroidRuntime(9724):    at android.widget.TextView.setText(TextView.java:3837)
   AndroidRuntime(9724):    at com.example.calllogproject.MainActivity$MainHandler.handleMessage(MainActivity.java:30)
   AndroidRuntime(9724):    at android.os.Handler.dispatchMessage(Handler.java:99)
   AndroidRuntime(9724):    at android.os.Looper.loop(Looper.java:137)
   AndroidRuntime(9724):    at android.app.ActivityThread.main(ActivityThread.java:5103)
   AndroidRuntime(9724):    at java.lang.reflect.Method.invokeNative(Native Method)
   AndroidRuntime(9724):    at java.lang.reflect.Method.invoke(Method.java:525)
   AndroidRuntime(9724):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
   AndroidRuntime(9724):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
   AndroidRuntime(9724):    at dalvik.system.NativeStart.main(Native Method)
java
android
handler
android-orientation
asked on Stack Overflow Jan 3, 2014 by vamsiampolu

1 Answer

0

Try this class:

class MainHandler extends Handler
{
    @Override
    public void handleMessage(Message msg)
    {
        switch(msg.what)
        {
            case MSG_ORIENTATION:                   
                if(tv!=null)
                    tv.setText(String.valueOf(msg.arg1));
                break;
            default:
                if(tv!=null)
                tv.setText("Nothing to see here");
        }
    }
}

I've changed

tv.setText(msg.arg1);

into

tv.setText(String.valueOf(msg.arg1));
answered on Stack Overflow Jan 3, 2014 by Dries

User contributions licensed under CC BY-SA 3.0