ANR在某些设备上启动活动时

时间:2012-05-17 17:25:52

标签: android android-intent surfaceview android-anr-dialog

我在某些设备上有一个错误,有时会在开始新活动时提供ANR。 我的设备(三星Galaxy S2)有一点滞后但我从来没有得到过ANR。有时获得ANR的其他设备是:HTC Desire HD和Xperia(我知道)。 我认为它可能是某些cpu密集的东西我做的那样使旧手机比新手机更加滞后,但我不知道。我使用Asynctask来加载所有重物并且ui线程确实做不了多少。

继承代码

我在这里创建一个单独的线程来访问共享首选项。

  @Override
    protected void onResume() {
        super.onResume();
        sound = false;
        Thread prefThread = new Thread(new Runnable(){
            public void run(){
                SharedPreferences prefs = getSharedPreferences(SharedPrefValues.OPTION_PREF, Context.MODE_PRIVATE);
                sound = prefs.getBoolean("sound", true);
                String difficulty = prefs.getString(SharedPrefValues.DIFFICULTY, SharedPrefValues.DIF_MEDIUM);
                prefs = getSharedPreferences(SharedPrefValues.HIGHSCORE_PREF, Context.MODE_PRIVATE);
                for(int i = 0; i < mapPnl.buttons.size(); i++)
                    mapPnl.SetRank(mapPnl.buttons.get(i), prefs.getString(SharedPrefValues.HIGHSCORE_RANK_NAME + difficulty + mapPnl.buttons.get(i).getName(), SharedPrefValues.RANK_NOT_OK));

            }
        });
        prefThread.start();
        mapPnl.speedX = 0;
    }

bgTask是一个AsyncTask。

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                WindowManager.LayoutParams.FLAG_FULLSCREEN); 


        Display display = getWindowManager().getDefaultDisplay();
        screenSize = new Point();
        screenSize.x = display.getWidth();
        screenSize.y = display.getHeight();


        mapPnl = new MapPnl(this);

        LoadBitmapsStuff bgTask = new LoadBitmapsStuff(this);
        bgTask.execute();

        addContentView(mapPnl, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    }

这是Surfaceview,可以绘制所有内容并处理onTouchEvents

private class MapPnl extends SurfaceView implements SurfaceHolder.Callback{
...
public MapPnl(Context context) {
            super(context);
            getHolder().addCallback(this);

            buttons = new ArrayList<ButtonMap>();

            posX = 0;
            speedX = 0;
            maxX = 0;

            drawThread = new DrawThread(getHolder());
            drawThread.setRunning(true);
        }
....
}

我在surfaceCreated

中启动drawThread
    public void surfaceCreated(SurfaceHolder arg0) {
        if(drawThread.getState() == Thread.State.NEW)
            drawThread.start();
        else if(drawThread.getState() == Thread.State.TERMINATED){
            drawThread = new DrawThread(getHolder());
            drawThread.setRunning(true);
            drawThread.start();
        }
    }

Surfacedestroyed来自网络指南

    public void surfaceDestroyed(SurfaceHolder arg0) {
        boolean retry = true;
        drawThread.setRunning(false);
        while (retry) {
            try {
                drawThread.join();
                retry = false;
            } catch (InterruptedException e) {
                // we will try it again and again...
            }
        }

    }

我认为这是重要的代码。 据我所知,UIthread仅通过这些部分运行(可能不是ondestroy,但我读到有人在另一个线程中遇到了问题。)。这应该在大多数系统上运行得非常快。

drawthread只显示一个文本,表示“加载”,直到asyncTask完成运行。因此,当一切都被加载和绘制时,用户开始触摸屏幕,那就是当ANR出现时。有什么提示吗?

更新(12-05-18): 我设法从模拟器中获取了一个logcat。我使用了一个缓慢的计算机,当我使用快速计算机时,A​​NR没有显示出来。

PID 860是活动

03-31 01:41:03.453: I/Process(71): Sending signal. PID: 860 SIG: 3
03-31 01:41:03.453: I/dalvikvm(860): threadid=3: reacting to signal 3
03-31 01:41:03.672: I/dalvikvm(860): Wrote stack traces to '/data/anr/traces.txt'
03-31 01:41:03.672: I/Process(71): Sending signal. PID: 71 SIG: 3
03-31 01:41:03.682: I/dalvikvm(71): threadid=3: reacting to signal 3
03-31 01:41:03.992: I/dalvikvm(71): Wrote stack traces to '/data/anr/traces.txt'
03-31 01:41:03.992: I/Process(71): Sending signal. PID: 381 SIG: 3
03-31 01:41:03.992: I/dalvikvm(381): threadid=3: reacting to signal 3
03-31 01:41:04.212: I/Process(71): Sending signal. PID: 349 SIG: 3
03-31 01:41:04.212: I/dalvikvm(349): threadid=3: reacting to signal 3
03-31 01:41:04.322: I/dalvikvm(349): Wrote stack traces to '/data/anr/traces.txt'
03-31 01:41:04.332: I/Process(71): Sending signal. PID: 664 SIG: 3
03-31 01:41:04.364: I/dalvikvm(664): threadid=3: reacting to signal 3
03-31 01:41:04.542: I/Process(71): Sending signal. PID: 364 SIG: 3
03-31 01:41:04.612: I/dalvikvm(364): threadid=3: reacting to signal 3
03-31 01:41:04.744: I/Process(71): Sending signal. PID: 371 SIG: 3
03-31 01:41:04.953: I/Process(71): Sending signal. PID: 822 SIG: 3
03-31 01:41:05.162: I/Process(71): Sending signal. PID: 139 SIG: 3
03-31 01:41:05.162: I/dalvikvm(139): threadid=3: reacting to signal 3
03-31 01:41:05.221: I/dalvikvm(822): threadid=3: reacting to signal 3
03-31 01:41:05.292: I/dalvikvm(371): threadid=3: reacting to signal 3
03-31 01:41:05.312: I/dalvikvm(139): Wrote stack traces to '/data/anr/traces.txt'
03-31 01:41:05.402: E/ActivityManager(71): ANR in se.blogspot.developingjsa.view (se.blogspot.developingjsa.view/.FirstMenu)
03-31 01:41:05.402: E/ActivityManager(71): Reason: keyDispatchingTimedOut
03-31 01:41:05.402: E/ActivityManager(71): Load: 1.79 / 1.63 / 1.31
03-31 01:41:05.402: E/ActivityManager(71): CPU usage from 5383ms to 64ms ago:
03-31 01:41:05.402: E/ActivityManager(71):   elopingjsa.view: 51% = 49% user + 1% kernel / faults: 36 minor 7 major
03-31 01:41:05.402: E/ActivityManager(71):   system_server: 44% = 41% user + 3% kernel / faults: 133 minor 1 major
03-31 01:41:05.402: E/ActivityManager(71):   adbd: 1% = 0% user + 0% kernel
03-31 01:41:05.402: E/ActivityManager(71):   m.android.phone: 1% = 0% user + 0% kernel / faults: 112 minor
03-31 01:41:05.402: E/ActivityManager(71):   ronsoft.openwnn: 0% = 0% user + 0% kernel / faults: 22 minor
03-31 01:41:05.402: E/ActivityManager(71):   com.svox.pico: 0% = 0% user + 0% kernel / faults: 7 minor
03-31 01:41:05.402: E/ActivityManager(71):   logcat: 0% = 0% user + 0% kernel
03-31 01:41:05.402: E/ActivityManager(71):   qemud: 0% = 0% user + 0% kernel
03-31 01:41:05.402: E/ActivityManager(71):   id.defcontainer: 0% = 0% user + 0% kernel / faults: 6 minor
03-31 01:41:05.402: E/ActivityManager(71):   ackageinstaller: 0% = 0% user + 0% kernel / faults: 5 minor
03-31 01:41:05.402: E/ActivityManager(71): TOTAL: 100% = 91% user + 8% kernel + 0% irq + 0% softirq

1 个答案:

答案 0 :(得分:0)

现在解决了。

我能够使用谷歌播放从这些设备获得堆栈跟踪。事实证明,我在那里发布的代码没有任何问题。

我使用的是教程中的代码(http://www.droidnova.com/playing-with-graphics-in-android-part-i,147.html)。每当onTouchEvent发生时,该代码都试图从drawThread获取surfaceHolder。

我猜有时drawThread在它的循环内部并且当UiTread试图抓住它时无法释放surfaceHolder,导致ANR。