2010-12-05 95 views
0

在學習android並且遵循教程開發一個小費計算器,它是一個非常好的教程,但是已經完成它,沒有什麼事情發生時,我突然在模擬器中運行它,只是得到一個力量關閉或空白屏幕(這是相當不可預測的)。我該如何解決這個隱藏的android代碼錯誤?

當我調試時,我可以得到的唯一線索是以下消息: [2010-12-05 22:02:01 - 提示計算器] ActivityManager:警告:活動未開始,其當前任務已被帶到前面

我的代碼如下,任何助手或指針如何我可以開始調試/修復這將是最感謝!

package com.tipcalc; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.view.KeyEvent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.View.OnKeyListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.RadioGroup; 
import android.widget.TextView; 
import android.widget.RadioGroup.OnCheckedChangeListener; 

public class tipCalc extends Activity { 

// Widgets in the Application 
private EditText txtAmount; 
private EditText txtPeople; 
private EditText txtTipOther; 
private RadioGroup rdoGroupTips; 
private Button btnCalculate; 
private Button btnReset; 

private TextView txtTipAmount; 
private TextView txtTotalToPay; 
private TextView txtTipPerPerson; 

// For the id of radio button selected 
private int radioCheckedId = -1; 



/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    // Access the various widgets by their id in R.java 
    txtAmount = (EditText) findViewById(R.id.txtAmount); 
    // On app load, the cursor should be in the amount field 
    txtAmount.requestFocus(); 

    txtPeople = (EditText) findViewById(R.id.txtPeople); 
    txtTipOther = (EditText) findViewById(R.id.txtTipOther); 

    rdoGroupTips = (RadioGroup) findViewById(R.id.rdoGroupTips); 

    btnCalculate = (Button) findViewById(R.id.btnCalculate); 

    // On app load the calculate button is disabled 
    btnCalculate.setEnabled(false); 

    /* 
    * Attach an OnCheckedChangeListener to the radio 
    * Group to monitor the radio buttons selected by user 
    * 
    */ 

    rdoGroupTips.setOnCheckedChangeListener(new OnCheckedChangeListener(){ 

    @Override 
    public void onCheckedChanged(RadioGroup group, int checkedId){ 
    // Enable/Disable Other Percentage tip field 
    if (checkedId == R.id.Radio15 || checkedId == R.id.Radio20){ 
     txtTipOther.setEnabled(false); 

     // Also enable the calculate button if Total Amount and no of people fields 
     // have valid values. 
     btnCalculate.setEnabled(txtAmount.getText().length() > 0 
      && txtPeople.getText().length() > 0 
      ); 

    } 

    if (checkedId == R.id.RadioOther){ 
     // enable the other % tip field 
     txtTipOther.setEnabled(true); 
     // set the focus to this field 
     txtTipOther.requestFocus(); 

     /* 
     * Enable the calc button if total amount and no of people 
     * fields have valid values. Also ensure that the user has 
     * entered an Other Tip % Value before enabling the calc button 
     */ 

     btnCalculate.setEnabled(
     txtAmount.getText().length() > 0 
     && txtPeople.getText().length() > 0 
     && txtTipOther.getText().length() > 0 
    ); 

     // work out the % rate chosen by the user 
     radioCheckedId = checkedId; 

    } 


    }}); 

/* 
* Create a KeyListener to the tip amount, no of people and other tip % fields 
* 
*/ 
txtAmount.setOnKeyListener(mKeyListener); 
txtPeople.setOnKeyListener(mKeyListener); 
txtTipOther.setOnKeyListener(mKeyListener); 

// Attach listener to the Calc and Reset Buttons 
btnCalculate.setOnClickListener(mClickListener); 
btnReset.setOnClickListener(mClickListener); 
} 

/* 
* OnKeyListener Special Routine 
*/ 

private OnKeyListener mKeyListener = new OnKeyListener() { 
@Override 
public boolean onKey(View v, int keyCode, KeyEvent event) { 
    switch (v.getId()) { 
    case R.id.txtAmount: 
    case R.id.txtPeople: 
    btnCalculate.setEnabled(txtAmount.getText().length() > 0 
    && txtPeople.getText().length() > 0); 
    break; 
    case R.id.txtTipOther: 
    btnCalculate.setEnabled(txtAmount.getText().length() > 0 
    && txtPeople.getText().length() > 0 
    && txtTipOther.getText().length() > 0); 
    break; 
    } 
    return false; 

    } 
}; 


// ClickListener for the calc and reset buttons 
private OnClickListener mClickListener = new OnClickListener(){ 
    @Override 
    public void onClick(View v){ 
    if(v.getId() == R.id.btnCalculate){ 
    calculate(); 
    } else { 
    reset(); 
    } 
    } 

}; 

    // Resets the results text views at the bottom of the screen as well as resets the text fields and radio buttons 
    private void reset() { 
    txtTipAmount.setText(""); 
    txtTotalToPay.setText(""); 
    txtTipPerPerson.setText(""); 
    txtAmount.setText(""); 
    txtPeople.setText(""); 
    txtTipOther.setText(""); 
    rdoGroupTips.clearCheck(); 
    rdoGroupTips.check(R.id.Radio15); 
    // set focus on the first field 
    txtAmount.requestFocus(); 
} 

    // Calculate the tip as per data entered by the user 
    private void calculate() { 
    Double billAmount = Double.parseDouble(
    txtAmount.getText().toString()); 
    Double totalPeople = Double.parseDouble(
    txtPeople.getText().toString()); 
    Double percentage = null; 
    boolean isError = false; 
    if (billAmount < 1.0) { 
    showErrorAlert("Enter a vlaid Total Amount.", 
    txtAmount.getId()); 
    isError = true; 
    } 

    if (totalPeople < 1.0) { 
    showErrorAlert("Enter a valid value for No. of people.", 
    txtPeople.getId()); 
    isError = true; 
    } 


    // If user never changes radio, then verify 15% is correct 
    if (radioCheckedId == -1) { 
    radioCheckedId = rdoGroupTips.getCheckedRadioButtonId(); 
    } 

    if (radioCheckedId == R.id.Radio15) { 
    percentage = 15.00; 
    } else if (radioCheckedId == R.id.Radio20) { 
    percentage = 20.00; 
    } else if (radioCheckedId == R.id.RadioOther) { 
    percentage = Double.parseDouble(
    txtTipOther.getText().toString()); 
    if (percentage < 1.0) { 
    showErrorAlert("Enter a valid Tip percentage", 
     txtTipOther.getId()); 
    isError = true; 
    } 

    } 

    // If all fields are valid, then calc the tips 
    if(!isError) { 
    Double tipAmount = ((billAmount * percentage)/100); 
    Double totalToPay = billAmount + tipAmount; 
    Double perPersonPays = totalToPay/totalPeople; 

    txtTipAmount.setText(tipAmount.toString()); 
    txtTotalToPay.setText(totalToPay.toString()); 
    txtTipPerPerson.setText(perPersonPays.toString()); 
    };} 



    // Show the alerts dialogs 
    // @param errorMessage - String the Error message to show 
    // @param fieldId - id of the field which caused the error 

    private void showErrorAlert(String errorMessage, 
    final int fieldId) { 
    new AlertDialog.Builder(this).setTitle("Error") 
    .setMessage(errorMessage).setNeutralButton("Close", 
    new DialogInterface.OnClickListener() { 

    @Override 
    public void onClick(DialogInterface dialog, 
     int which) { 
     findViewById(fieldId).requestFocus(); 
    } 
    }).show(); 


}; 

} 

回答

0

「活動未開始,其當前任務已提前」僅表示該活動未重新安裝,並且也未真正重新啓動,但仍在後臺運行,這就是爲什麼只是將其帶回到前端。您可以強制模擬器通過使用Android調試橋(ADB)重新啓動應用程序,並終止與您的活動關聯的流程。

這就是你的第一個問題的答案。爲了解決這個問題,有必要在發佈強制關閉消息時發佈你的堆棧跟蹤。你可以從Logcat複製這個堆棧跟蹤。

2

該警告意味着它沒有安裝新版本,因爲您的代碼沒有從模擬器上的版本更改。您可以直接進入應用程序抽屜,然後再次運行該應用程序以將其帶到最前面,或對代碼進行更改並重新運行。

此外,您發佈的WAY代碼太多太多以至於您的問題的描述很少。

1

如果你的程序沒有在仿真器上運行,並且你得到「強制關閉或空白屏幕」,你可能沒有正確地遵循教程。您可能在清單文件中缺少一些權限,或者可能沒有可繪製文件夾中所需的圖像或任何其他錯誤。你必須給出你的錯誤的日誌貓輸出。

其次,如果您獲得「ActivityManager:警告:活動未啓動,它的當前任務已經到達前臺」,並且再次運行並安裝程序。快速入門是在你的代碼示例「abbbbb」中添加一些字母,然後刪除它們,再次保存你的程序,然後重新運行它。

希望它有幫助:D

相關問題