我得到了“只有创建了视图层次结构的原始线程才能接触到它的视图”。因为我在使用"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");
}
}有没有人知道如何避免这个错误?
谢谢
发布于 2012-02-20 06:39:32
这里有一个可能对你有用的解决方案:
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);
}所以你要保证你的视图被原始线程所触动。
发布于 2012-10-18 18:09:21
这个解决方案的很大一部分可以在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();
}
}https://stackoverflow.com/questions/9353326
复制相似问题