Java 处理程序不工作
我使用了Java 处理程序不工作,java,android,handler,android-orientation,Java,Android,Handler,Android Orientation,我使用了OrientationEventListener,它与处理程序相结合,在屏幕旋转时更新TextView: MainHandler handler; TextView tv; MyOrientationEventListener listener; class MainHandler extends Handler { @Override public void handleMessage(Message msg) { switch(msg.wh
OrientationEventListener
,它与处理程序相结合,在屏幕旋转时更新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);
}
我正在使用onResume
中的listener.enable()
和listener.disable()
在调用它们各自的超类方法后在onPause
方法中。当我尝试旋转屏幕并进行舍入时:
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)
尝试以下课程:
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))代码>因此方向Venthandler检测到屏幕方向和活动方向之间的差异???@ZouZou它工作了,简单而且非常愚蠢,我在CameraActivity
中添加了一个修改版本的代码,这进一步减缓了方向的更改,发布了一个问题:使用字符串是否有效。valueOf
?因为实际上您调用了抛出异常的方法setText(intresid)
。。。
tv.setText(String.valueOf(msg.arg1));