2012-08-08 64 views
0

我需要写像一本书的Android应用程序。我有大约100个图像,我需要向后,向前和另一个按钮显示它们。 我试图为每个图像创建一个XML布局并取得图像布局的背景。交换XML布局导致崩溃的Android

当运行应用程序,如果我交换XML的布局过程中按按钮快速,程序崩溃。如果我减少影像尺寸我的问题也减少了。不幸的是,为了解决这个问题,我需要另一个解决方案,因为我不能使用更小的图像尺寸,但我仍然有崩溃问题。

+0

您需要从LogCat中为您的崩溃发布一些代码和堆栈跟踪。 – kcoppock 2012-08-08 17:52:50

回答

1

有一个布局,在它的ImageView。然后,每当需要循环显示下一张或上一张图像时,请不断更改图像视图的源图像。问题的

0

部分原因是,点击一个按钮,用户界面立即返回/队列点击,即使与点击相关的行动尚未完成。由于超出本回复范围的原因,其值得注意的是,仅仅在“做功”时停用按钮是无效的。有一对夫妇解决这样的问题:一个是使用底层的“工作”已经完成之后被只设置一个布尔标志。然后,按钮操作处理程序之内,你无视发生的按钮点击之前的标志复位:

/** 
    * Button presses are ignored unless idle. 
    */ 
    private void onMyButtonClicked() { 
     if(idle) { 
     doWork(); 
     } 
    } 

    /** 
    * Does some work and then restores idle state when finished. 
    */ 
    private void doWork() { 
     idle = false; 
     // maybe you spin off a worker thread or something else. 
     // the important thing is that either in that thread's run() or maybe just in the body of 
     // this doWork() method, you: 
     idle = true; 
    } 

另一种通用的方法是使用时间过滤;即。您设定上限,其中按下按钮的最高频率为1Hz:

/** 
    * Determines whether or not a button press should be acted upon. Note that this method 
    * can be used within any interactive widget's onAction method, not just buttons. This kind of 
    * filtering is necessary due to the way that Android caches button clicks before processing them. 
    * See http://code.google.com/p/android/issues/detail?id=20073 
    * @param timestamp timestamp of the button press in question 
    * @return True if the timing of this button press falls within the specified threshold 
    */ 
    public static synchronized boolean validateButtonPress(long timestamp) { 
     long delta = timestamp - lastButtonPress; 
     lastButtonPress = timestamp; 
     return delta > BUTTON_PRESS_THRESHOLD_MS; 
    } 

然后你会做这样的事情:

private void onMyButtonClicked() {  
     if(validateButtonPress(System.currentTimeMillis())) { 
     doWork(); 
     } 
    } 

这最后的解决方案是公认的不确定性,但如果你考虑到用户在移动设备上几乎从不故意点击按钮的次数超过每秒1-2次,但并不是那么糟糕。