2010-12-05 15 views
0

アンドロイドを学んでいて、ヒント計算機を開発するチュートリアルに従っていますが、それは本当に良いチュートリアルでしたが、それを完了しましたが、エミュレータで突然実行すると何も起こりません。それはかなり予測できない)。隠されているこのアンドロイドコードのバグを修正するにはどうすればよいですか?

デバッグ時に私が得ることができる唯一の手がかりは、次のメッセージです。 [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 Debug Bridge(ADB)を使用してエミュレータにアプリを再起動させ、アクティビティに関連付けられているプロセスを強制終了することができます。

あなたの最初の質問に対する答えです。この問題を解決するには、強制終了メッセージが表示されたときにスタックトレースを送信する必要があります。このスタックトレースをLogcatからコピーできます。

2

この警告は、エミュレータ上のバージョンからコードが変更されていないため、新しいバージョンをインストールしなかったことを意味します。アプリの引き出しに移動してアプリケーションをもう一度起動すると、コードを前面に表示したり、コードを変更して再実行したりすることができます。

また、あなたの問題の小さな説明のために、あまりにも多くのコードを投稿しました。

1

プログラムがエミュレータで実行されておらず、「強制終了または空白の画面」が表示される場合は、チュートリアルを正しく実行していない可能性があります。マニフェストファイルにいくつかのパーミッションがないか、またはドロウアブルフォルダに画像が必要でない、またはその他の間違いがあるかもしれません。あなたはあなたのエラーのログcatの出力を与える必要があります。

第2に、「ActivityManager:警告:アクティビティが開始されておらず、現在のタスクが前面に表示されている」場合は、プログラムを再度実行してインストールします。クイックハックは、コード例 "abbbbb"にアルファベットを追加してから削除し、プログラムをもう一度保存してから実行し直すことです。

希望するもの:

関連する問題