2012-02-19 27 views
1

我得到了「只有創建視圖層次結構的原始線程才能觸及其視圖。」因爲我在使用「onUtteranceCompleted」時使用了文本到語音轉換,並在內部對TextView進行了一些調用。文本到語音轉換時出錯:「只有創建視圖層次結構的原始線程可以觸摸其視圖。」

下面是我的一些代碼:

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

} 

會有人知道如何避免這個錯誤嗎?

謝謝

+0

發現嘗試txtCurrentWord.setText( 「世界你好」);沒有'這個'。 – Urban 2012-02-19 22:03:45

+0

不會出現同樣的錯誤:( – xtrimsky 2012-02-19 22:33:34

回答

3

下面是可能會爲你工作的解決方案:

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

所以,你保證你的觀點是原來的線程感動。

0

解決方案的很大一部分可以在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(); 
    } 
} 
+1

嘗試並將答案放在答案中,然後保留該鏈接作爲參考 – 2012-10-18 10:16:24

+0

請總結鏈接的內容;只是發佈鏈接是沒有幫助的 – LittleBobbyTables 2012-10-18 10:16:49

+0

添加我的代碼,請參閱上面的內容 – 2012-10-18 12:59:58

相關問題