2012-12-12 121 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; 
} 
相關問題