首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >文本到语音转换时出错:“只有创建视图层次结构的原始线程才能接触它的视图。”

文本到语音转换时出错:“只有创建视图层次结构的原始线程才能接触它的视图。”
EN

Stack Overflow用户
提问于 2012-02-20 05:15:37
回答 2查看 305关注 0票数 1

我得到了“只有创建了视图层次结构的原始线程才能接触到它的视图”。因为我在使用"onUtteranceCompleted“的同时使用了文本到语音转换,并且在内部调用了一些TextView。

下面是我的一些代码:

代码语言:javascript
复制
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");
}

}

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

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-02-20 06:39:32

这里有一个可能对你有用的解决方案:

代码语言:javascript
复制
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);
}

所以你要保证你的视图被原始线程所触动。

票数 3
EN

Stack Overflow用户

发布于 2012-10-18 18:09:21

这个解决方案的很大一部分可以在http://www.helloandroid.com/tutorials/using-threads-and-progressdialog上找到

代码语言:javascript
复制
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();
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9353326

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档