我的活动看起来有点像: 请不要复制, 它需要来自另一个类的三个变量并计算(a,b),(b,c),(c,d),(a,b,c)的gcd,然后对该等式进行分解并给出五个不同的textViews字符串。 如果你能帮助改进x.square到x 2 ,请帮助我,欢迎所有建议。
package com.aditya.blabla;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class AlternateForms extends Activity {
AnotherClass ec = new AnotherClass ();
int aa = ec.geta();
int bb = ec.getb();
int cc = ec.getc();
int abhcf = HCF(aa, cc), bchcf = HCF(cc, cc), achcf = HCF(aa, cc),
abchcf = HCF(abhcf, cc);
String altform1,altform2,altform3,altform4,altform5;
TextView t1, t2, t3, t4, t5;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.alternate);
t1 = (TextView) findViewById(R.id.textView1);
t2 = (TextView) findViewById(R.id.textView2);
t3 = (TextView) findViewById(R.id.textView3);
t4 = (TextView) findViewById(R.id.textView4);
t5 = (TextView) findViewById(R.id.textView5);
altform1 = abhcf + "x(" + aa / abhcf + "x+" + bb / abhcf + ")+"
+ cc;
setProgress(20);
altform2 = aa + "x.square" + bchcf + "(" + bb / bchcf + "x" + cc
/ bchcf + ")" + cc;
setProgress(30);
altform3 = achcf + "(" + aa / achcf + "x.square" + bb / achcf
+ ")" + cc + "x";
setProgress(40);
altform4 = abchcf + "(" + aa / abchcf + "x.square" + bb / abchcf
+ "x" + cc / abchcf + ")";
setProgress(50);
altform5 = abchcf + "(" + "x" + "(" + aa / abchcf + "x" + bb
/ abchcf + ")" + cc / abchcf + ")";
if (abhcf != 1) {
t1.setText(altform1);
}
if (bchcf != 1) {
t2.setText(altform2);
}
if (achcf != 1) {
t3.setText(altform3);
}
if (abchcf != 1) {
t4.setText(altform4);
t5.setText(altform5);
}
}
private int HCF(int m, int n) {
// TODO Auto-generated method stub
int hcf = 0;
while (n != 0) {
hcf = n;
n = m % n;
n = hcf;
}
return hcf;
}
}
它没有错误但它需要很多进程并且看起来像挂起并给出强制关闭对话框 有什么建议可以处理吗?
答案 0 :(得分:1)
将HCF(m, n)
方法中的所有来电doInBackground(Void... params)
放入AsyncTask
。将altformN
方法中的所有onCreate(Bundle s)
分配也放在doInBackground(Void... params)
中。将if
的所有setText(CharSequence s)
语句放在TextView
onPostExecute(Void nothing)
方法的AsyncTask
方法中onPostExecute(Void nothing)
。您可能知道,AsyncTask
中的doInBackground(Void... params)
方法在UI线程上执行,而public class AlternateFormsTask extends AsyncTask<Void, Void, Void> {
int abhcf, bchcf, achcf, abchcf;
int aa, bb, cc;
AnotherClass ec = new AnotherClass ();
@Override
public Void doInBackground(Void.... params){
aa = ec.geta();
bb = ec.getb();
cc = ec.getc();
// ...insert all your calls to HCF(m, n) here
return null
}
// If all the processing in doInBackground really takes that long
// you may want to intersperse some calls to AsyncTask's publishProgress
// to update the user as to the progress of the app, otherwise it will appear
// just as hung as before, just without the FC dialogs.
@Override
public void onPostExecute(Void nothing){
//....do all your setText stuff here
}
}
在单独的线程上执行。
这是一个骨架实现:
{{1}}
答案 1 :(得分:0)
当需要大量时间时,不使用Threads
,它将始终显示为“挂起”。使用Threads
并在完成后更新GUI。
您应该在MainThread
答案 2 :(得分:0)
hcf方法应更改为
static int HCF(int a, int b) {
while (b != 0) {
int t = b;
b = a % b;
a = t;
}
return a;
}