我有一个android线程类,必须更改外部TextView的值,但它不会更改它:S。我对其进行了调试,可以看到线程执行了更改TextView值的行,但是该行未反映在该值上。

public class AugmentedRealitySampleActivity extends Activity {

    private TextView tv2;
public void onCreate(Bundle savedInstanceState)
{
       super.onCreate(savedInstanceState);
       tv2=new TextView(getApplicationContext());
       tv2.setText("Test2");
       myThread t = new myThread();
       t.start();
}

 public class myThread extends Thread
 {
    public void run()
    {
        Looper.prepare();
        tv2.append("\n AAA");
        Looper.loop();
    }
 }
}


编辑;以完整模式发布的代码:

private class myAsyncTask extends AsyncTask<Void, Void, Void>
    {
        boolean condition = true;

        protected Void doInBackground(Void... params)
        {
            while( condition )
            try
            {
                publishProgress();
                Thread.sleep( 1000 );
            }catch (InterruptedException e) {e.printStackTrace();}
            return null;
        }

        protected void onProgressUpdate(Void... values)
        {
            //Update your TextView
            if (!mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) //si el GPS está desactivado
            {
                boolean visible=false;
                float a = 80; //a=bearingTo
                float b =azimuth;
                float d = Math.abs((a - b)) % 360;
                float r = d > 180 ? 360 - d : d;

                if (Math.abs(r)<21 && Math.abs(inclination)<30)
                    visible=true;

                if (visible)
                {
                    tv2.append("\nIs Visible?: true "+ r);
                    poi.setVisibility(View.VISIBLE);

                    ////////CALCULAMOS COORDENADA X DEL POI///////
                    float leftSideFovDegree=azimuth-21;
                    if (leftSideFovDegree<0)
                        leftSideFovDegree+=360;
                    float dist=anglesDistance(leftSideFovDegree,a);
                    float xCoordPercent=(dist*100)/42; //xCoordPercent es el porcentaje de X respecto al horizontal de la pantalla, en el que estará el objeto
                    if (a<0)//si el bearingTo es negativo, entonces la cordenada X se invierte
                        xCoordPercent=100-xCoordPercent;

                    tv2.append("\nxCoordPercent: "+ xCoordPercent);

                    ////////CALCULAMOS COORDENADA Y DEL POI///////
                    //float yCoordPercent= (float)(42.7377 * Math.pow(Math.E,(0.0450962*inclination)));
                    float yCoordPercent= (float)(33.0459 * Math.pow(Math.E,(0.056327*inclination)));
                    tv2.append("\nyCoordPercent: "+ yCoordPercent);

                    Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
                    int w = display.getWidth();
                    int h = display.getHeight();

                    RelativeLayout.LayoutParams position = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                    position.leftMargin = (int)(w*(xCoordPercent/100));
                    position.topMargin  = (int)(h*(yCoordPercent/100));
                    poi.setLayoutParams(position);
                }
                else
                {
                    tv2.append("\nIs Visible?: false "+ r);
                    poi.setVisibility(View.GONE);
                }
            }
        }
    }

最佳答案

这可能是因为您的线程不是UI线程。视图的所有更新必须在UI线程上完成。

尝试这样做:

runOnUiThread( new Runnable() {
  @Override
  public void run() {
    tv2.append("\n AAA");
  }
});


或更妙的是-将您的线程转换为AsyncTask,如下所示:

private class myAsyncTask extends AsyncTask<Void, Void, Void> {
            boolean condition = true;

    @Override
    protected Void doInBackground(Void... params) {
        while( condition )
         try {
            // Do your calculations
            publishProgress();
            Thread.sleep( 200 );
         } catch (InterruptedException e) {
            e.printStackTrace();
         }

        return null;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        //Update your TextView
    }
}


而不是创建一个新线程,您要做的是:

new myAsyncTask().execute();

09-30 14:05
查看更多