0

我创建了一个 android 应用程序,用户在其中输入一个字符串和一个数字,其中该数字将被计算为其阶乘。一切正常,但是当我将代码包含在线程中时,没有输出。

这是我的源代码:

package com.inputandfactorial;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

    Button chkCmd;
    EditText input1, input2;
    TextView display1, display2;
    int res = 1,factint;
    String fact;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        chkCmd = (Button) findViewById(R.id.bProcess);
        input1 = (EditText) findViewById(R.id.etString);
        input2 = (EditText) findViewById(R.id.etFactorial);
        display1 = (TextView) findViewById(R.id.tvString);
        display2 = (TextView) findViewById(R.id.tvFactorial);

        chkCmd.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {



                Thread t1 = new Thread(){
                    public void run(){
                        String change = input1.getText().toString();
                        fact = input2.getText().toString();
                        factint = Integer.parseInt(fact);
                        for (int i = 1; i <= factint; i++) {
                            res = res * i;
                        }
                        try {
                            display2.setText("The factorial of " + fact + " is " + res);
                            display1.setText("The string value is " + change);
                        } catch (Exception e) {

                        }
                    }
                };
                t1.start();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

我知道我不需要把它放在一个线程中,我只是在做实验,看看这是否可行,但它没有。

4

4 回答 4

0

您不能从线程对 UI 进行更改。UI 上的所有更改都需要在 UIThread 上完成。要轻松做到这一点,请使用 AsyncTask :

http://developer.android.com/reference/android/os/AsyncTask.html

于 2012-08-01T15:00:12.527 回答
0

我同意上面关于您不需要线程的评论但是为了回答您的问题...

You need to look at AsyncTasks. I wrote a previous answer that should provide you with a working example AsyncTask Android example

It goes on to explain you can only affect the UI from the original GUI thread therefore your code above will not work.

于 2012-08-01T15:01:15.600 回答
0

You can only manipulate Views on the UI thread. Wrap your code in a Runnable and execute it on the main thread with Activity#runOnUiThread(Runnable action):

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        display2.setText("The factorial of " + fact + " is " + res);
        display1.setText("The string value is " + change);
    }
}

That said, simple arithmetic doesn't warrant the use of a Thread in the first place...

于 2012-08-01T15:04:02.407 回答
0

1. Its always a good practice to keep the UI work on the UI thread, and Non-UI work on the Non-UI thread. That became a law with the release of HoneyComb Android Version.

2. An android application starts on the UI thread, creating any other thread will drop you out of the Android thread, and this thread will be the Non-UI thread.

3. To post the work from the Non-UI thread back on the UI thread, we need to use these 2 ways:

- Thread along with Handler.

- AsyncTask, which is specially introduced in Android to sync UI and Non-UI thread, Its also known as Painless threading.

于 2012-08-01T16:50:10.243 回答