Android 无法立即更新textview

Android 无法立即更新textview,android,user-interface,textview,Android,User Interface,Textview,我正在制作一个翻译应用程序,我想在其中更新一个文本视图,但它没有按照我想要的做 final audioTranslate at[] = new audioTranslate[1]; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_audio_translate); tv = (T

我正在制作一个翻译应用程序,我想在其中更新一个文本视图,但它没有按照我想要的做

final audioTranslate at[] = new audioTranslate[1];
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_audio_translate);
    tv = (TextView) findViewById(R.id.textView11);
    spokentext = (TextView) findViewById(R.id.textView12);
    translatedtext = (TextView) findViewById(R.id.textView13);
    at[0] = this;
    start = (Button) findViewById(R.id.Start);
    start.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Button b = (Button) v;
            if (b.getText().toString().equals("Start")) {
                //code
            }
            else{
                tv.setText("");  //these all 3 operations are happening after transfer method is finished.
                b.setText("Wait");  
                b.setEnabled(false);
                transfer();
            }
        }
    });
}

void transfer() {
    Thread t = new Thread() {
        public void run() {
            //socket connection
            //it is giving exception on below line too.
            //Only the original thread that created a view hierarchy can touch its views. 
            //java.lang.RuntimeException
            at[0].spokentext.setText(spoken);
        }
    };
t.start();
}
这是我的布局文件供参考:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_audio_translate"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="diverse.technologies.transcriber.audioTranslate">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_horizontal">

            <Button
                android:text="Start"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/Start" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="20dp"
            android:gravity="center_horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/textView11"
                android:textColor="@color/colorAccent"
                android:textSize="24sp" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/textView12"
                android:textSize="26sp"
                android:fontFamily="serif-monospace"
                android:textStyle="normal|bold" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="5px">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/textView13"
                android:textSize="24sp"/>
        </TableRow>

    </TableLayout>
</ScrollView>

</RelativeLayout>


我不明白上面的例外有什么问题。我正在使用具有textview的视图对象。请提供帮助。

普通工作线程无法修改诸如TextView之类的UI元素。在[0].spokentext.setText(口语)上运行代码
内部UI线程。在您的run方法中写入

runOnUiThread(new Runnable() {
                @Override
                public void run() {//Your code}}

但是,我不推荐这种类型的本机java编码。使用处理程序或异步任务将其用于传输方法:

void transfer() {
Thread t = new Thread() {
    public void run() {
        //socket connection
        //it is giving exception on below line too.
        //Only the original thread that created a view hierarchy can touch its views. 
        //java.lang.RuntimeException
        spokentext.post(new Runnable() {
        @Override
        public void run() {
            at[0].spokentext.setText(spoken);
        }
    });

    }
};
t.start();
}

就像@Pavneet_Singh所说的,你不能从另一个线程更新你的UI。您可能希望这样做,而不是调用
transfer()

public class YourActivity {
    ...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Button b = (Button) v;
                if (b.getText().toString().equals("Start")) {
                    //code
                } else {
                    tv.setText("");  //these all 3 operations are happening after transfer method is finished.
                    b.setText("Wait");  
                    b.setEnabled(false);
                    new RetrieveSpokenTextTask().execute();
                }
            }
        });
    }

    private class RetrieveSpokenTextTask extends AsyncTask<Void,Void,String> {
        @Override
        public String doInBackground(Void... params) {
            String spoken = "the value you would like to assign to the TextView";
            return spoken;
        }

        @Override
        public void postExecute(String text) {
            // the `text` variable has now the string you returned in `doInBackground`
            spokentext.setText(text);
        }
    }
}
公共类活动{
...
@凌驾
创建时受保护的void(Bundle savedInstanceState){
...
start.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
按钮b=(按钮)v;
如果(b.getText().toString().equals(“开始”)){
//代码
}否则{
tv.setText(“”;//这三个操作都是在传输方法完成后进行的。
b、 setText(“等待”);
b、 setEnabled(假);
新建RetrieveSpokenTextTask().execute();
}
}
});
}
私有类RetrieveSpokenTextTask扩展了AsyncTask{
@凌驾
公共字符串doInBackground(Void…params){
String speaked=“要分配给TextView的值”;
回话;
}
@凌驾
公共void postExecute(字符串文本){
//'text'变量现在具有您在'doInBackground'中返回的字符串`
spokentext.setText(文本);
}
}
}

您能解释问题的确切原因吗?您不能从worker Thread更新
UI
。我想在执行纹理tv的代码时更新纹理tv。我无法使用audiotranslate类本身的对象从线程更新名为SpokentText的textview@Pavneet_SinghBut我在这个应用程序的其他活动中也做了同样的事情。它在那里工作得很好。但这里不是。活动和线程是不同的。活动可以直接修改UI。好的,我试试。当代码开始执行时,我如何立即更新文本框中的文本?这应该立即发布,而不是PostDelayed,所以你的意思是我也应该在我的textview“tv”上使用post方法。根据您的代码,如果按钮文本不等于“开始”,则应在单击按钮后立即将tv设置为空,否则无法正常工作。在传输方法完成后,它将设置为空。@abhishek panjabi如果它甚至不调用
t.start()
inside
transfer()
,您如何执行生成异常的命令。很抱歉,我没有足够的声誉,所以我不能直接对你的帖子发表评论。它当然叫t.start man。我忘了在问题中输入那一行。你试过使用异步任务吗?也许这就是你想用的。您可以在
doInBackground
方法中执行长时间运行的任务,然后在
postExecute
方法中执行用户界面中的更改。@abhishek panjabi我编辑了我的答案,以防您想检查代码中AsyncTask的用法。