文本转语音时出错:"Only the original thread that created a view hierarchy can touch its views."



我得到了"只有创建视图层次结构的原始线程才能接触到它的视图。"因为我在使用"onUtteranceCompleted"和内部对TextView进行一些调用时使用了Text to speech。

这是我的一些代码:

public class MyActivity extends Activity implements OnInitListener, OnUtteranceCompletedListener {
private TextView txtCurrentWord;
public void onCreate(Bundle savedInstanceState) {
    ...
    this.txtCurrentWord = (TextView) findViewById(R.id.txtCurrentWord);
}
public void onUtteranceCompleted(String uttId) {
    this.txtCurrentWord.setText("hello world");
}
}

有人知道如何避免这个错误吗?

感谢

这里有一个可能适用于您的解决方案:

private Handler viewHandler;
public void onCreate(Bundle savedInstanceState) {
   ...
   viewHandler = new Handler();
   ...
...
public void onUtteranceCompleted(String uttId) {
    Runnable run = new Runnable() {
        public void run() {
            txtCurrentWord.setText("hello world");
        }
    };
    viewHandler.post(run);
}

因此,您保证您的视图被原始线程所触动。

可以在http://www.helloandroid.com/tutorials/using-threads-and-progressdialog

package any....;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class CopyOfActivityConfigs extends Activity implements Runnable
 {
   Context context = this;
   public  static ProgressDialog  progressSpinner;
   public         TableLayout     tableLayoutAppsProtect;
   public         TableLayout     tableLayoutAppsProtectIn;
   final Handler handler = new Handler()
    {
      @Override
      public void handleMessage( Message message)
       {
         String sResult = (String) message.obj;
         if( (sResult != null) && (sResult != ""))
           {
             tableLayoutAppsProtect = (TableLayout) findViewById( R.id.tableLayoutAppsProtect);
             tableLayoutAppsProtect.addView( tableLayoutAppsProtectIn);
             if( progressSpinner != null)  progressSpinner.dismiss();
           }
         return;
       }
    };

   public void run()
    {
      final Message message = handler.obtainMessage( 1, fThreadAppsProtectListGenerate( context));
      handler.sendMessage( message);
    }

   public String fThreadAppsProtectListGenerate( Context context)
    {
      tableLayoutAppsProtectIn = new TableLayout( context);
      if( tableLayoutAppsProtectIn != null)  tableLayoutAppsProtectIn.removeAllViews();
             TableRow tableRow = new TableRow( context);
             LinearLayout linearLayout = new LinearLayout( context);
             TextView textViewSiteNow = new TextView( context);
             textViewSiteNow.setText( "...");
             linearLayout.addView( textViewSiteNow);
             tableRow.addView( linearLayout);
             tableLayoutAppsProtectIn.addView( tableRow);
      if( progressSpinner != null)  progressSpinner.dismiss();
      return "any";
    }

   @Override
   public void onCreate( Bundle savedInstanceState) 
    {
      super.onCreate( savedInstanceState);
      setContentView( R.layout.configappsprotect);
      progressSpinner = new ProgressDialog( this);
      progressSpinner.setMessage( getString( R.string.sSpinnerAppsProtectListGenerate));
      progressSpinner.show();
      Thread thread = new Thread( this);
      thread.start();
    }
}

相关内容

最新更新