【发布时间】:2016-03-15 15:27:07
【问题描述】:
我开始学习 android,我的第一个练习是计算器。 在做正确的计算器之前,我计划创建一个非常基本的应用程序,其中用户只需通过 EditText 输入 2 个数字,使用 Buttons 选择算术运算,并在按下“igual”按钮时在 TextView 中获取结果。
我的想法是声明 2 个字符串(sinput1 和 sinput2),它们从每个 EditText(num1 和 num2)中获取文本,然后将字符串解析为整数变量(input1 和 input2)。
在我开始对操作的运行方式进行编码之前,所有布局看起来和工作正常,但是每次我运行应用程序(使用手机或模拟器)时,当我按下 EditText 输入数字时它就会崩溃。我会把我的代码放在这里,也许我的错误在别处。
我认为改变开关在 onClick void 上的工作方式可以解决这个问题,部分原因是因为之前我有一系列的 if 并且应用程序在加载布局之前就崩溃了。但现在我被困在了这一点上。
package com.example.caye.colores;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
Button mas;
Button menos;
Button por;
Button div;
Button igual;
EditText num1;
EditText num2;
TextView letrero1;
TextView letrero2;
TextView resultado;
int input1;
int input2;
String sinput1;
String sinput2;
int operacion;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mas = (Button)this.findViewById(R.id.mas);
mas.setOnClickListener(this);
mas.setText("+");
menos = (Button)this.findViewById(R.id.menos);
menos.setOnClickListener(this);
menos.setText("-");
por = (Button)this.findViewById(R.id.por);
por.setOnClickListener(this);
por.setText("*");
div = (Button)this.findViewById(R.id.div);
div.setOnClickListener(this);
div.setText("/");
igual = (Button)this.findViewById(R.id.igual);
igual.setOnClickListener(this);
igual.setText("=");
num1 = (EditText)this.findViewById(R.id.num1);
num1.setOnClickListener(this);
num2 = (EditText)this.findViewById(R.id.num2);
num2.setOnClickListener(this);
letrero1 = (TextView)this.findViewById(R.id.letrero1);
letrero1.setText("Número 1");
letrero2 = (TextView)this.findViewById(R.id.letrero2);
letrero2.setText("Número 2");
resultado = (TextView)this.findViewById(R.id.resultado);
resultado.setText("");
}
public void onClick(View view){
switch (view.getId()){
case R.id.mas:
operacion = 1;
break;
case R.id.menos:
operacion = 2;
break;
case R.id.por:
operacion = 3;
break;
case R.id.div:
operacion = 4;
break;
case R.id.num1:
sinput1 = num1.getText().toString();
input1 = Integer.parseInt(sinput1);
break;
case R.id.num2:
sinput2 = num2.getText().toString();
input2 = Integer.parseInt(sinput2);
break;
case R.id.resultado:
switch (operacion){
case 1:
resultado.setText(input1 + input2);
break;
case 2:
resultado.setText(input1 - input2);
break;
case 3:
resultado.setText(input1 * input2);
break;
case 4:
resultado.setText(input1 / input2);
break;
default:
resultado.setText("Elige una operación");
break;
}
break;
}
}
}
这里。
03-15 09:41:24.234 22041-22041/? D/dalvikvm: Late-enabling CheckJNI
03-15 09:41:24.334 22041-22041/com.example.caye.colores D/HyLog: I : /data/font/config/dfactpre.dat, No such file or directory (2)
03-15 09:41:24.338 22041-22041/com.example.caye.colores D/asset: AssetManager-->addDefaultAssets CIP path not exsit!
03-15 09:41:24.502 22041-22041/com.example.caye.colores W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
03-15 09:41:24.502 22041-22041/com.example.caye.colores I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested
03-15 09:41:24.502 22041-22041/com.example.caye.colores W/dalvikvm: VFY: unable to resolve interface method 15038: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
03-15 09:41:24.502 22041-22041/com.example.caye.colores D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
03-15 09:41:24.503 22041-22041/com.example.caye.colores I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode
03-15 09:41:24.503 22041-22041/com.example.caye.colores W/dalvikvm: VFY: unable to resolve interface method 15042: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
03-15 09:41:24.503 22041-22041/com.example.caye.colores D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
03-15 09:41:24.580 22041-22041/com.example.caye.colores I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations
03-15 09:41:24.580 22041-22041/com.example.caye.colores W/dalvikvm: VFY: unable to resolve virtual method 396: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
03-15 09:41:24.580 22041-22041/com.example.caye.colores D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
03-15 09:41:24.581 22041-22041/com.example.caye.colores I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType
03-15 09:41:24.581 22041-22041/com.example.caye.colores W/dalvikvm: VFY: unable to resolve virtual method 418: Landroid/content/res/TypedArray;.getType (I)I
03-15 09:41:24.581 22041-22041/com.example.caye.colores D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
03-15 09:41:24.690 22041-22041/com.example.caye.colores D/BubblePopupHelper: isShowingBubblePopup : false
03-15 09:41:24.690 22041-22041/com.example.caye.colores D/BubblePopupHelper: isShowingBubblePopup : false
03-15 09:41:24.838 22041-22041/com.example.caye.colores D/GraphicBuffer: create handle(0x619ca788) (w:544, h:960, f:1)
03-15 09:41:24.844 22041-22041/com.example.caye.colores D/OpenGLRenderer: Enabling debug mode 0
03-15 09:41:24.846 22041-22041/com.example.caye.colores D/GraphicBuffer: create handle(0x61bf9318) (w:1216, h:832, f:1)
03-15 09:41:24.857 22041-22041/com.example.caye.colores D/OpenGLRenderer: setViewport 540x960 <0x619ca8c0>
03-15 09:41:24.888 22041-22041/com.example.caye.colores D/BubblePopupHelper: isShowingBubblePopup : false
03-15 09:41:24.888 22041-22041/com.example.caye.colores D/BubblePopupHelper: isShowingBubblePopup : false
03-15 09:41:24.890 22041-22041/com.example.caye.colores D/BubblePopupHelper: isShowingBubblePopup : false
03-15 09:41:24.890 22041-22041/com.example.caye.colores D/BubblePopupHelper: isShowingBubblePopup : false
03-15 09:41:24.946 22041-22041/com.example.caye.colores D/cliptray_Editor: setInputTypeforClipTray(): 0
03-15 09:41:24.960 22041-22041/com.example.caye.colores D/BubblePopupHelper: isShowingBubblePopup : false
03-15 09:41:24.960 22041-22041/com.example.caye.colores D/BubblePopupHelper: isShowingBubblePopup : false
03-15 09:41:24.960 22041-22041/com.example.caye.colores D/BubblePopupHelper: isShowingBubblePopup : false
03-15 09:41:24.961 22041-22041/com.example.caye.colores D/BubblePopupHelper: isShowingBubblePopup : false
03-15 09:41:25.008 22041-22041/com.example.caye.colores D/cliptray_Editor: setInputTypeforClipTray(): 0
03-15 09:41:25.446 22041-22041/com.example.caye.colores D/GraphicBuffer: create handle(0x62130980) (w:544, h:960, f:1)
03-15 09:41:25.957 22041-22041/com.example.caye.colores D/GraphicBuffer: create handle(0x6201a318) (w:544, h:960, f:1)
03-15 09:41:28.168 22041-22041/com.example.caye.colores I/ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWN
03-15 09:41:28.316 22041-22041/com.example.caye.colores I/ViewRootImpl: ViewRoot's Touch Event : ACTION_UP
03-15 09:41:28.341 22041-22041/com.example.caye.colores V/Provider/Settings: get setting for user 0 by user 0 so skipping cache
03-15 09:41:28.342 22041-22041/com.example.caye.colores V/Provider/Settings: invalidate [system]: current 32 != cached 0
03-15 09:41:28.347 22041-22041/com.example.caye.colores D/AndroidRuntime: Shutting down VM
03-15 09:41:28.347 22041-22041/com.example.caye.colores W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x4249be48)
03-15 09:41:28.355 22041-22041/com.example.caye.colores E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.caye.colores, PID: 22041
java.lang.NumberFormatException: Invalid int: ""
at java.lang.Integer.invalidInt(Integer.java:137)
at java.lang.Integer.parseInt(Integer.java:358)
at java.lang.Integer.parseInt(Integer.java:331)
at com.example.caye.colores.MainActivity.onClick(MainActivity.java:81)
at android.view.View.performClick(View.java:4461)
at android.view.View$PerformClick.run(View.java:18523)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5118)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
at dalvik.system.NativeStart.main(Native Method)
03-15 09:41:30.416 22041-22041/com.example.caye.colores I/Process: Sending signal. PID: 22041 SIG: 9
【问题讨论】:
-
它崩溃了!所以发布堆栈跟踪。您还调用检查用户是否使用
if(!TextUtils.isEmpty("your string")在editText 中输入任何内容以确保安全 -
第二次从按钮点击上的editText获取值,因为当你点击你的editText时它会聚焦,以便你输入一些东西
-
也使用 try 语句来防止崩溃并能够解决确切的问题以进行故障排除。
标签: java android string parsing math