2012-12-12 84 views
0

我有一个看起来有点像的活动: 请不要复制它, 它从另一个类取三个变量并计算(a,b),(b,c),(c, d),(a,b,c),然后分解公式并给出五个不同的textViews字符串。 也如果你可以帮助改善x.square到x ,请帮助我,欢迎所有的建议。阻止应用程序被挂起

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

} 

它没有错误,但它需要很多的过程,看起来像挂起,并给出强制关闭对话框 任何建议来处理呢?

+1

只是抬起头来。通过发布这里你已经在[Creative-Commons Sharealike](http://creativecommons.org/licenses/by-sa/3.0/)许可它明确规定其他人可以自由复制和使用它...找到链接在此页面的右下角以了解更多信息。 – FoamyGuy

+0

没关系,我可以制造出这样的军队 – RE60K

+0

产生一个军队....?什么? – FoamyGuy

回答

1

投入HCF(m, n)。同样将altformN的所有分配从onCreate(Bundle s)方法分配到doInBackground(Void... params)。把所有的if陈述你setText(CharSequence s)TextViewonPostExecute(Void nothing)方法那AsyncTask。您可能知道,AsyncTask中的onPostExecute(Void nothing)方法在UI线程上执行,而doInBackground(Void... params)在单独的线程上执行。

这里是一个框架实现:

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 
    } 
} 
+0

对不起,主要原因是应该是hcf方法: – RE60K

+0

'static int HCF(int a,int b){ \t \t while(b!= 0){ \t \t \t int t = b; \t \t \t b = a%b; \t \t \t a = t; \t \t} \t \t return a; \t}' – RE60K

+0

但是这对于异步任务是正确的..如果你可以帮助将x.square改进为x2,请帮助我 – RE60K

0

当它需要很多时间时,它总是会显示“挂起”而不使用Threads。使用Threads并在完成后更新您的GUI。 你应该在AsyncTaskdoInBackground(Void... params)方法的所有调用做尽可能少的工作在MainThread

+0

我应该尝试异步任务 – RE60K

+0

是的。这将是一个解决方案 – Sprigg

+0

,但它仍然挂起..你可以给这个场景的布局吗?因为我想在即时创建异步任务错误 – RE60K

0

的HCF方法应改为

static int HCF(int a, int b) { 
while (b != 0) { 
int t = b; 
b = a % b; 
a = t; 
} 
return a; 
} 
相关问题