2009-08-30 8 views
0

Ainsi c'est mon premier vrai programme d'androïde (! Bonjour monde), mais j'ai l'expérience de Java. Le programme compile bien, mais en l'exécutant se bloque dès qu'il s'ouvre (a essayé le débogage , mais il se bloque avant qu'il ne frappe mon point d'arrêt). Était à la recherche de conseils de quelqu'un qui est plus expérimenté avec Android.android programme s'écraser (nouveau à plate-forme)

package org.me.tipcalculator; 
import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TextView; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import java.text.NumberFormat; 
import android.util.Log; 

public class TipCalculator extends Activity { 

public static final String tag = "TipCalculator"; 

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

    final EditText mealpricefield = (EditText) findViewById(R.id.mealprice); 
    final TextView answerfield = (TextView) findViewById(R.id.answer); 

    final Button button = (Button) findViewById(R.id.calculate); 
    button.setOnClickListener(new Button.OnClickListener() 
    { 
     public void onClick(View v) { 
      try 
      { 
       Log.i(tag, "onClick invoked."); 
       String mealprice = mealpricefield.getText().toString(); 

       Log.i(tag, "mealprice is [" + mealprice + "]"); 
       String answer = ""; 

       if (mealprice.indexOf("$") == -1) 
       { 
        mealprice = "$" + mealprice; 
       } 

       float fmp = 0.0F; 

       NumberFormat nf = 
         java.text.NumberFormat.getCurrencyInstance(); 

       fmp = nf.parse(mealprice).floatValue(); 

       fmp *= 1.2; 

       Log.i(tag, "Total Meal Price (unformatted) is [" + fmp + "]"); 

       answer = "Full Price, including 20% Tip: " + nf.format(fmp); 

       answerfield.setText(answer); 

       Log.i(tag, "onClick Complete"); 

      } 
      catch(java.text.ParseException pe){ 
       Log.i (tag ,"Parse exception caught"); 
       answerfield.setText("Failed to parse amount?"); 
      } 
      catch(Exception e){ 
       Log.e (tag ,"Failed to Calculate Tip:" + e.getMessage()); 
       e.printStackTrace(); 
       answerfield.setText(e.getMessage()); 
      } 
     } 
    } 
    ); 
} 

Juste au cas où il aide le xml Heres

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Android Tip Calculator"/> 
<EditText 
    android:id="@+id/mealprice" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:autoText="true"/> 
<Button 
    android:id="@+id/calculate" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Calculate Tip"/> 
<TextView 
    android:id= "@+id/answer" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text=""/> 
</LinearLayout> 

Répondre

0

Vous pouvez oublier d'enregistrer votre nom d'activité dans le fichier AndroidManifest.xml. C'est une erreur très commune pour les débutants. AndroidManifest.xml devrait ressembler à ceci:

<activity android:name=".TipCalculator" 
       android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
1

Utilisez adb logcat, DDMS, ou la perspective DDMS dans Eclipse pour trouver la trace de la pile Java qui a causé votre accident.

Questions connexes