android Textviews得到冻结

时间:2013-09-09 08:53:42

标签: android textview

有时我的textview在最初的几次之后就会卡住,有人可以告诉我为什么......我启动了这个帖子因为我听说你应该只在某些线程中更新文本所以有人能告诉我为什么这个会破坏吗?

Runnable updateTextRunnable=new Runnable(){  
      public void run() {  
          count++;

                Location predicationPoint = DOTGpsAppUtils.predictionAlgorithm(latitude, longitude, bearing, distance);
                Location currentLocation = new Location("current");
                currentLocation.setLatitude(latitude);
                currentLocation.setLongitude(longitude);

                distance = DOTGpsAppUtils.distanceBetweenTwoLocations(currentLocation, predicationPoint);
                bearing = DOTGpsAppUtils.headingBetweenTwoLocations(currentLocation, predicationPoint);

                double predictionLongitude = (longitude + predicationPoint.getLongitude())/2;
                double predictionLatitude = (latitude + predicationPoint.getLatitude())/2;

                TextView textView = (TextView) findViewById(R.id.oneHundredMillisecondLatitude);
                textView.setText(Double.valueOf(predictionLatitude).toString());

                textView = (TextView) findViewById(R.id.oneHundredMillisecondLongitude);
                textView.setText(Double.valueOf(predictionLongitude).toString());

                textView = (TextView) findViewById(R.id.heading);
                textView.setText(Double.valueOf(bearing).toString());

                textView = (TextView) findViewById(R.id.speed);
                textView.setText(Double.valueOf(distance).toString());

                if(count >= 100)
                {
                    count = 0;
                }

                longitude = predictionLongitude;
                latitude = predictionLatitude;


          handler.postDelayed(this, TIME_DELAY);  
         }  
     }; 

logcat的:

 09-09 02:13:21.049: E/Trace(26898): error opening trace file: No such file or directory (2)
       09-09 02:05:02.942: W/IInputConnectionWrapper(25579): getExtractedText on   inactive InputConnection
       09-09 02:05:02.983: W/IInputConnectionWrapper(25579): getExtractedText on inactive InputConnection
       09-09 02:05:03.003: W/IInputConnectionWrapper(25579): getExtractedText on inactive InputConnection
       09-09 02:05:25.216: D/dalvikvm(25579): GC_CONCURRENT freed 7915K, 52% free 8261K/16963K, paused 17ms+3ms, total 86ms
       09-09 02:05:35.237: I/dalvikvm(25773): Turning on JNI app bug workarounds for target SDK version 7...
       09-09 02:05:35.247: E/Trace(25773): error opening trace file: No such file or directory (2)
       09-09 02:05:35.257: D/ActivityThread(25773): setTargetHeapUtilization:0.25
       09-09 02:05:35.257: D/ActivityThread(25773): setTargetHeapIdealFree:8388608
       09-09 02:05:35.257: D/ActivityThread(25773): setTargetHeapConcurrentStart:2097152

1 个答案:

答案 0 :(得分:0)

您必须在UI线程中调用UI方法。 Android doc的规则是

  

不要从UI线程外部访问Android UI工具包

将updateTextRunnable代码更改为

final TextView latitudeTextView = (TextView) findViewById(R.id.oneHundredMillisecondLatitude);

Runnable updateTextRunnable=new Runnable(){  
  public void run() {  
      count++;

            Location predicationPoint = DOTGpsAppUtils.predictionAlgorithm(latitude, longitude, bearing, distance);
            Location currentLocation = new Location("current");
            currentLocation.setLatitude(latitude);
            currentLocation.setLongitude(longitude);

            distance = DOTGpsAppUtils.distanceBetweenTwoLocations(currentLocation, predicationPoint);
            bearing = DOTGpsAppUtils.headingBetweenTwoLocations(currentLocation, predicationPoint);

            double predictionLongitude = (longitude + predicationPoint.getLongitude())/2;
            double predictionLatitude = (latitude + predicationPoint.getLatitude())/2;

            latitudeTextView.post(new Runnable() {
                public void run() {
                    latitudeTextView.setText(Double.valueOf(predictionLatitude).toString());
                }  
            });

            [...]

            if(count >= 100)
            {
                count = 0;
            }

            longitude = predictionLongitude;
            latitude = predictionLatitude;


      handler.postDelayed(this, TIME_DELAY);  
     }  
 }; 

您可以在此处找到有关Android文档的更多说明http://developer.android.com/guide/components/processes-and-threads.html#WorkerThreads

希望这有帮助。