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.