有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java处理程序不工作

我使用了OrientationEventListener和一个Handler在屏幕旋转时更新一个TextView

  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");
        }
    }
}

这是我的OrientationEventListener这里:

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;
    }

}

我正在使用onCreate中的处理程序将我的处理程序注册到UI线程,因此它可以用来更新屏幕上的视图。但是,该代码似乎不起作用:

    @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);
}

在调用它们各自的超类方法之后,我在{}和{}中使用了{}和{}方法。当我试图旋转屏幕时,会出现四舍五入:

   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): 安卓.content.res.Resources$NotFoundException: String resource ID #0x5a
   AndroidRuntime(9724):    at 安卓.content.res.Resources.getText(Resources.java:239)
   AndroidRuntime(9724):    at 安卓.widget.TextView.setText(TextView.java:3837)
   AndroidRuntime(9724):    at com.example.calllogproject.MainActivity$MainHandler.handleMessage(MainActivity.java:30)
   AndroidRuntime(9724):    at 安卓.os.Handler.dispatchMessage(Handler.java:99)
   AndroidRuntime(9724):    at 安卓.os.Looper.loop(Looper.java:137)
   AndroidRuntime(9724):    at 安卓.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.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
   AndroidRuntime(9724):    at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:553)
   AndroidRuntime(9724):    at dalvik.system.NativeStart.main(Native Method)

共 (1) 个答案

  1. # 1 楼答案

    试试这个课程:

    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");
            }
        }
    }
    

    我变了

    tv.setText(msg.arg1);
    

    进入

    tv.setText(String.valueOf(msg.arg1));