android 程序崩溃(平台新手)

发布于 2024-08-02 21:09:41 字数 3197 浏览 4 评论 0原文

所以这是我的第一个真正的Android程序(!hello world),但我确实有java经验。该程序编译得很好,但是在运行时它一打开就崩溃了(尝试过调试,但它在到达我的断点之前崩溃了)。正在寻找对 Android 更有经验的人的任何建议。

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());
            }
        }
    }
    );
}

以防万一它有帮助,这是 xml

<?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>

So it is my first real Android program (!hello world), but i do have java experience.The program compiles fine, but on running it crashes as soon as it opens (tried debugging, but it crashes before it hits my breakpoint). Was looking for any advice from anyone who is more experienced with android.

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());
            }
        }
    }
    );
}

Just in case it helps heres the xml

<?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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伪心 2024-08-09 21:09:41

使用 adb logcat、DDMS 或 Eclipse 中的 DDMS 透视图查找导致崩溃的 Java 堆栈跟踪。

Use adb logcat, DDMS, or the DDMS perspective in Eclipse to find the Java stack trace that caused your crash.

在梵高的星空下 2024-08-09 21:09:41

您可能忘记在 AndroidManifest.xml 中注册您的 Activity 名称。对于初学者来说,这是一个非常常见的错误。 AndroidManifest.xml 应该是这样的:

    <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>

You may forget to register your activity name in the AndroidManifest.xml. It is a very common mistake for starters. AndroidManifest.xml should be like this:

    <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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文