My Fragmant包含一个SensorEventListener,它应该通过LightSensor评估一些输入。
LightDisp是我的视图。我认为崩溃是因为Fragment已经关闭但是LightSensor触发了Fragment来执行这个仍在工作的方法:
private void setBackground(int Backgroundcolor) {
Resources r = getResources();
Drawable[] layers = new Drawable[2];
layers[0] = new ColorDrawable(Color.rgb(Backgroundcolor,
Backgroundcolor, Backgroundcolor));
layers[1] = r.getDrawable(R.drawable.screen);
LayerDrawable layerDrawable = new LayerDrawable(layers);
LightDisp.setBackgroundDrawable(layerDrawable);
LightDisp.invalidate();
}
如何避免这种情况? 使用LightDisp.post()方法也没有解决问题。 PS:我设置延迟到sensorListener的速度越慢,崩溃越少。使用SENSOR_DELAY_FASTEST时,它几乎每次都会崩溃。
logcat的:
10-15 15:36:46.361: D/SensorManager(10304): registerListener :: handle = 3 name= GP2A Light Sensor delay= 10000 Listener= ch.steff_94.morseapp_beta.Input_lightsensor$1@418d52d8
10-15 15:36:46.366: E/SensorManager(10304): thread start
10-15 15:36:46.366: E/SensorManager(10304): =======>>> Sensor Thread Running <<<========
10-15 15:36:52.436: D/dalvikvm(10304): GC_CONCURRENT freed 390K, 4% free 14754K/15367K, paused 1ms+4ms
10-15 15:36:58.971: D/SensorManager(10304): unregisterListener:: handle = 3 Listener= ch.steff_94.morseapp_beta.Input_lightsensor$1@418d52d8 name = GP2A Light Sensor
10-15 15:37:06.711: D/AndroidRuntime(10304): Shutting down VM
10-15 15:37:06.711: W/dalvikvm(10304): threadid=1: thread exiting with uncaught exception (group=0x40c5d1f8)
10-15 15:37:06.756: E/AndroidRuntime(10304): FATAL EXCEPTION: main
10-15 15:37:06.756: E/AndroidRuntime(10304): java.lang.IllegalStateException: Fragment Input_lightsensor{418d4c00} not attached to Activity
10-15 15:37:06.756: E/AndroidRuntime(10304): at android.app.Fragment.getResources(Fragment.java:741)
10-15 15:37:06.756: E/AndroidRuntime(10304): at ch.steff_94.morseapp_beta.Input_lightsensor.setBackground(Input_lightsensor.java:133)
10-15 15:37:06.756: E/AndroidRuntime(10304): at ch.steff_94.morseapp_beta.Input_lightsensor.access$2(Input_lightsensor.java:132)
10-15 15:37:06.756: E/AndroidRuntime(10304): at ch.steff_94.morseapp_beta.Input_lightsensor$1.onSensorChanged(Input_lightsensor.java:281)
10-15 15:37:06.756: E/AndroidRuntime(10304): at android.hardware.SensorManager$ListenerDelegate$1.handleMessage(SensorManager.java:635)
10-15 15:37:06.756: E/AndroidRuntime(10304): at android.os.Handler.dispatchMessage(Handler.java:99)
10-15 15:37:06.756: E/AndroidRuntime(10304): at android.os.Looper.loop(Looper.java:137)
10-15 15:37:06.756: E/AndroidRuntime(10304): at android.app.ActivityThread.main(ActivityThread.java:4511)
10-15 15:37:06.756: E/AndroidRuntime(10304): at java.lang.reflect.Method.invokeNative(Native Method)
10-15 15:37:06.756: E/AndroidRuntime(10304): at java.lang.reflect.Method.invoke(Method.java:511)
10-15 15:37:06.756: E/AndroidRuntime(10304): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
10-15 15:37:06.756: E/AndroidRuntime(10304): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
10-15 15:37:06.756: E/AndroidRuntime(10304): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
您收到的“片段未附加到活动”异常,因此我的猜测是您在创建Fragment
之前在Activity
中实例化了某些内容。弄清楚这可能是什么,并将其移至onActivityCreated()
方法。
答案 1 :(得分:0)
刚发现问题:
在我提交Listener
而不是FragmentTransaction
方法之前,我必须取消注册onPause()
;)