0

I've created an android app where the user inputs a string and a number, where the number will be calculated to its factorial. Everything's working fine but when i enclose my code inside the thread there's no output.

here's my source code:

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

I know i don't need to put it in a thread, i just was just experiment to see if this will work and it didn't.

philip
  • 1,292
  • 3
  • 24
  • 44
  • 3
    Don't make extra problems for yourself. Unless you need threads, don't use them. – Wug Aug 01 '12 at 15:02
  • I just wanna learn stuff, I might encounter this problem in the future, this will save me time and frustration. – philip Aug 01 '12 at 15:14

4 Answers4

0

You can't make change on the UI from a Thread. All changes on the UI need to be done on the UIThread. To do that easily, use an AsyncTask :

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

Aurélien Guillard
  • 1,203
  • 8
  • 20
0

I agree with the comment above about you not needing threading however in the interest of answering your question...

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.

Community
  • 1
  • 1
Graham Smith
  • 25,627
  • 10
  • 46
  • 69
  • If you use an `AsyncTask`, you should manipulate the UI with the task's results in `onPostExecute`. – Alex Lockwood Aug 01 '12 at 15:08
  • Please see my link in my answer - it explains all of this in greater detail http://stackoverflow.com/questions/9671546/asynctask-android-example/9671602#9671602 – Graham Smith Aug 01 '12 at 15:09
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...

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
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.

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75