我剛剛開始在Android中編碼。 我試圖創建一個計算兩個數字總和的應用程序。 即使在這個簡單的程序中也會發生錯誤。 應用程序顯示: 不幸的是,在仿真器中運行時,application_name已停止。Android_Beginner級別的計算器
package com.example.first;
import org.w3c.dom.Text;
import android.R.string;
import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements android.view.View.OnClickListener
{
Button buttonSum,buttonSumOne;
EditText textOne,textTwo;
TextView textSum;
float a,b,sum;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonSum=(Button)findViewById(R.id.button1);
buttonSum.setOnClickListener(this);
textOne=(EditText)findViewById(R.id.editText1);
textTwo=(EditText)findViewById(R.id.editText2);
textSum=(TextView)findViewById(R.id.textSum);
a=Float.parseFloat(textOne.getText().toString());
a=Float.valueOf(textOne.getText().toString());
b=Float.parseFloat(textTwo.getText().toString());
sum=a+b;
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(R.id.button1==v.getId())
textSum.setText(String.valueOf(sum));
}
}
的logcat:
03-16 07:08:16.297: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-16 07:08:16.316: W/Trace(791): Unexpected value from nativeGetEnabledTags: 0
03-16 07:08:18.496: D/AndroidRuntime(791): Shutting down VM
03-16 07:08:18.496: W/dalvikvm(791): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
03-16 07:08:18.536: E/AndroidRuntime(791): FATAL EXCEPTION: main
03-16 07:08:18.536: E/AndroidRuntime(791): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.first/com.example.first.MainActivity}: java.lang.NumberFormatException: Invalid float: ""
03-16 07:08:18.536: E/AndroidRuntime(791): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-16 07:08:18.536: E/AndroidRuntime(791): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-16 07:08:18.536: E/AndroidRuntime(791): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-16 07:08:18.536: E/AndroidRuntime(791): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-16 07:08:18.536: E/AndroidRuntime(791): at android.os.Handler.dispatchMessage(Handler.java:99)
03-16 07:08:18.536: E/AndroidRuntime(791): at android.os.Looper.loop(Looper.java:137)
03-16 07:08:18.536: E/AndroidRuntime(791): at android.app.ActivityThread.main(ActivityThread.java:5039)
03-16 07:08:18.536: E/AndroidRuntime(791): at java.lang.reflect.Method.invokeNative(Native Method)
03-16 07:08:18.536: E/AndroidRuntime(791): at java.lang.reflect.Method.invoke(Method.java:511)
03-16 07:08:18.536: E/AndroidRuntime(791): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-16 07:08:18.536: E/AndroidRuntime(791): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-16 07:08:18.536: E/AndroidRuntime(791): at dalvik.system.NativeStart.main(Native Method)
03-16 07:08:18.536: E/AndroidRuntime(791): Caused by: java.lang.NumberFormatException: Invalid float: ""
03-16 07:08:18.536: E/AndroidRuntime(791): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
03-16 07:08:18.536: E/AndroidRuntime(791): at java.lang.StringToReal.parseFloat(StringToReal.java:289)
03-16 07:08:18.536: E/AndroidRuntime(791): at java.lang.Float.parseFloat(Float.java:300)
03-16 07:08:18.536: E/AndroidRuntime(791): at com.example.first.MainActivity.onCreate(MainActivity.java:33)
03-16 07:08:18.536: E/AndroidRuntime(791): at android.app.Activity.performCreate(Activity.java:5104)
03-16 07:08:18.536: E/AndroidRuntime(791): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-16 07:08:18.536: E/AndroidRuntime(791): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-16 07:08:18.536: E/AndroidRuntime(791): ... 11 more
03-16 07:08:52.026: I/Process(791): Sending signal. PID: 791 SIG: 9
logcat說什麼? – 2013-03-16 07:14:20
在此處貼上你的錯誤 – 2013-03-16 07:14:29
感謝您的回覆Mr.Raghav Sood。 – 2013-03-16 07:16:08