ANR keyDispatchingTimedOut平台OTHER

时间:2013-01-06 20:38:05

标签: android

如何调查此错误?我从android市场读了这个日志。平台我看到“其他”。

Line 2637 and others:

    seekBar1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
              public void onStopTrackingTouch(SeekBar seekBar) {
              // TODO Auto-generated method stub
              if(seek<10)
              {
              seek=10;
              seekBar1.setProgress(seek);
              }
    //        Toast.makeText(getBaseContext(), String.valueOf(p), Toast.LENGTH_SHORT).show();
              }
              public void onStartTrackingTouch(SeekBar seekBar) {
              // TODO Auto-generated method stub
              }
              public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
              // TODO Auto-generated method stub
              seek=progress;
              SharedPreferences prefs = getSharedPreferences(PRIVATE_PREF, 0);
              SharedPreferences.Editor editor = prefs.edit();
              editor.putInt("seek", seek);
              editor.putInt("progress", progress);
              editor.commit();
              textView1.setTextSize(seek);
              }
              });


    "main" prio=5 tid=1 NATIVE
      | group="main" sCount=1 dsCount=0 obj=0x409f0460 self=0x12800
      | sysTid=12489 nice=0 sched=0/0 cgrp=[fopen-error:2] handle=1074803848
      | schedstat=( 1790000000 1790000000 3091 ) utm=159 stm=20 core=0
      at libcore.io.Posix.fsync(Native Method)
      at libcore.io.BlockGuardOs.fsync(BlockGuardOs.java:97)
      at java.io.FileDescriptor.sync(FileDescriptor.java:71)
      at android.os.FileUtils.sync(FileUtils.java:111)
      at android.app.SharedPreferencesImpl.writeToFile(SharedPreferencesImpl.java:577)
      at android.app.SharedPreferencesImpl.access$800(SharedPreferencesImpl.java:48)
      at android.app.SharedPreferencesImpl$2.run(SharedPreferencesImpl.java:491)
      at android.app.SharedPreferencesImpl.enqueueDiskWrite(SharedPreferencesImpl.java:512)
      at android.app.SharedPreferencesImpl.access$100(SharedPreferencesImpl.java:48)
      at android.app.SharedPreferencesImpl$EditorImpl.commit(SharedPreferencesImpl.java:434)
      at com.example.app.MainActivity$3.onProgressChanged(MainActivity.java:2637)
      at android.widget.SeekBar.onProgressRefresh(SeekBar.java:89)
      at android.widget.ProgressBar.doRefreshProgress(ProgressBar.java:609)
      at android.widget.ProgressBar.refreshProgress(ProgressBar.java:621)
      at android.widget.ProgressBar.setProgress(ProgressBar.java:670)
      at android.widget.AbsSeekBar.trackTouchEvent(AbsSeekBar.java:411)
      at android.widget.AbsSeekBar.onTouchEvent(AbsSeekBar.java:349)
      at android.view.View.dispatchTouchEvent(View.java:5545)
      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
      at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1912)
      at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1371)
      at android.app.Activity.dispatchTouchEvent(Activity.java:2364)
      at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1860)
      at android.view.View.dispatchPointerEvent(View.java:5725)
      at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2907)
      at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2471)
      at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:849)
      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:863)
      at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2446)
      at android.os.Handler.dispatchMessage(Handler.java:99)
      at android.os.Looper.loop(Looper.java:137)
      at android.app.ActivityThread.main(ActivityThread.java:4424)
      at java.lang.reflect.Method.invokeNative(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:511)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
      at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

这是一个非常老的问题,但答案可能仍然有用。

上面的代码中有两个问题:

  1. editor.commit(); commit()方法的调用是同步的,这意味着每次调用时,数据都会直接写入光盘。
  2. editor.commit();方法内调用onProgressChanged()。根据实现方式,此方法可能在一秒钟内被多次调用。将它与UI线程上的磁盘写操作结合在一起,就意味着您一直在写并且UI被阻止。

可能的解决方案:

  1. 将所需的值(“ seek”,“ progress”)保存到内存中。仅在存在数据丢失风险(例如活动状态更改)时才将它们保存到共享首选项中。

  2. editor.commit();替换为editor.apply();。 Apply()方法是异步的,因此阻塞UI线程的可能性很小。

您可以在此处查看完整的文档: https://developer.android.com/reference/android/content/SharedPreferences.Editor.html#apply()