2015-06-20 8 views
-1

我創建了一個簡單的android項目。但是,問題是所有3個活動中的第二個活動正在跳過。 logcat顯示幾個警告。我的android項目的活動正在跳過

"Skipped 108 frames! The application may be doing too much work on its main thread." 

我的第二個活動 -

EditText et1,et2,et3,et4; 
Button btn; 
int i,counter; 
double theory_credit,theory_gp,lab_credit,lab_gp,total_credit,total_gp,cgpa; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_another); 

    et1 = (EditText) findViewById(R.id.editText1); 
    et2 = (EditText) findViewById(R.id.editText2); 
    et3 = (EditText) findViewById(R.id.editText3); 
    et4 = (EditText) findViewById(R.id.editText4); 
    btn = (Button) findViewById(R.id.button1); 

    String value=getIntent().getStringExtra("Input"); 
    counter=Integer.parseInt(value); 
    theory_credit=theory_gp=lab_credit=lab_gp=total_credit=total_gp=cgpa=0.0; 

    for(i=1;i<=counter;i++){ 


     btn.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       String inp; 

       inp=et1.getText().toString(); 
       theory_credit=Double.parseDouble(inp); 
       et1.setText(""); 
       inp=et2.getText().toString(); 
       theory_gp=Double.parseDouble(inp); 
       et2.setText(""); 
       inp=et3.getText().toString(); 
       lab_credit=Double.parseDouble(inp); 
       et3.setText(""); 
       inp=et4.getText().toString(); 
       lab_gp=Double.parseDouble(inp); 
       et4.setText(""); 

       if(theory_gp>0.0 && theory_credit>0.0){ 
        total_credit+=theory_credit; 
        total_gp+=(theory_gp*theory_credit); 
       } 

       if(lab_gp>0.0 && lab_credit>0.0){ 
        total_credit+=lab_credit; 
        total_gp+=(lab_gp*lab_credit); 
       } 
      } 
     }); 
    } 

    if(total_credit>0.0){ 
     cgpa=total_gp/total_credit; 
    } 

    Intent intent=new Intent(AnotherActivity.this, FinalActivity.class); 
    intent.putExtra("Result", cgpa); 
    startActivity(intent); 
} 

我是如何解決這一問題? 我的第一和第三項活動運作良好。

+0

你在模擬器或真實設備上測試過嗎?大多數時候,這個錯誤是由於模擬器速度緩慢。 –

+0

在仿真器上或真實設備上運行應用程序時,您是否看到了這一點? – sandalone

+0

我在模擬器和真實設備上都嘗試過。但結果相同。 –

回答

2

沒有必要在一個java類中添加兩個佈局。每個佈局只與一個類相關聯。因此,在你的代碼中你已經寫了setContentView(R.id.yourLayout)兩次,這是不需要的。此外,在單個UI線程中,您不能放置兩個佈局,這也可能是導致崩潰的一個原因。

+0

如果我發表評論第一個 「setContentView(R.id.yourLayout)」 外部循環然後程序崩潰,如果我把那個語句,那麼它只是跳過第二個活動。 –

+0

如果您必須移動到另一個活動,請使用INTENTS,則您使用的方法不合適。並且在super.onCreate(savedInstanceState)函數後面設置內容視圖,它肯定會解決您的問題 –

+0

實際上,我在循環的每次迭代中都使用editText輸入了4個輸入。 因此我需要設置空白字段的輸入,所以我把這個說法 「setContentView(R.id.yourLayout)」。 接受最終輸入後​​,我將計算的值發送到下一個活動。用於設置空白字段的 –

0

嘗試從您的for循環中刪除setContentView(R.layout.activity_another);。你已經在上面設置了你的視圖。你有沒有把它放在裏面?

+0

即使在刪除該語句後也會得到相同的結果。 –

+0

,因爲最後的活動應該在終止循環之後開始。 –