1

我的 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 几乎每次都会崩溃。

日志猫:

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)
4

2 回答 2

1

你得到一个“片段未附加到活动”异常,所以我的猜测是你在创建Fragment之前实例化了你的东西Activity。弄清楚这可能是什么,然后将其移至您的onActivityCreated()方法。

于 2012-10-15T15:06:27.597 回答
0

刚刚发现问题:我必须在我提交之前取消注册,ListenerFragmentTransaction不是在onPause()方法中;)

于 2012-10-15T15:38:07.030 回答