0

有时我的文本视图在前几次后卡住了,谁能告诉我为什么.. 我开始这个线程是因为我听说你应该只更新某些线程中的文本,所以有人能告诉我为什么这个会坏吗?

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);  
         }  
     }; 

日志猫:

 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
4

1 回答 1

0

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

不要从 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

希望这可以帮助。

于 2013-09-09T09:53:54.757 回答