android 程序崩溃(平台新手)
所以这是我的第一个真正的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
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.您可能忘记在 AndroidManifest.xml 中注册您的 Activity 名称。对于初学者来说,这是一个非常常见的错误。 AndroidManifest.xml 应该是这样的:
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: