我在logcat中遇到致命异常错误,应用程序在运行时停止。
爪哇
package com.example.parth.editcal;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
public static int a,b,x,t1,t2 ;
public static EditText tt ;
public static TextView ans ;
/* int p = { a , b ,x }
if (p[4] == 0 && p[5] == 0){
p[1] = p[3];
p[4] = 1;
}
else if(p[4] == 1 && p[5] ==0 ){
p[1] = p[3];
p[5] = 1;
}
if (p[4] == 1 && p[5] == 1){
p[2] = p[3] ;
p[1] = p[1] + p[2] ;
p[5] = 2;
}
p[3] = 0 ;
*/
public static int logica(){
if (t1 == 0 && t2 == 0){
a = x;
t1 = 1;
}
else if(t1 == 1 && t2 ==0 ){
a = x;
t2 = 1;
}
if (t1 == 1 && t2 == 1){
b = x ;
a = a + b ;
t2 = 2 ;
}
x = 0;
return a;
}
public static int logics(){
if (t1 == 0 && t2 == 0){
a = x;
t1 = 1;
}
else if(t1 == 1 && t2 ==0 ){
a = x;
t2 = 1;
}
if (t1 == 1 && t2 == 1){
b = x ;
a = a - b ;
t2 = 2 ;
}
x = 0;
return a;
}
public static int logicm(){
if (t1 == 0 && t2 == 0){
a = x;
t1 = 1;
}
else if(t1 == 1 && t2 ==0 ){
a = x;
t2 = 1;
}
if (t1 == 1 && t2 == 1){
b = x ;
a = a * b ;
t2 = 2 ;
}
x = 0;
return a;
}
public static int logicd(){
if (t1 == 0 && t2 == 0){
a = x;
t1 = 1;
}
else if(t1 == 1 && t2 ==0 ){
a = x;
t2 = 1;
}
if (t1 == 1 && t2 == 1){
b = x ;
a = a / b ;
t2 = 2 ;
}
x = 0;
return a;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button add = (Button) findViewById(R.id.add);
Button sub = (Button) findViewById(R.id.sub);
Button mul = (Button) findViewById(R.id.mul);
Button div = (Button) findViewById(R.id.div);
Button eq = (Button) findViewById(R.id.eq);
tt = (EditText) findViewById(R.id.tt);
ans = (TextView) findViewById(R.id.ans);
if (x == 0){
x = Integer.parseInt(tt.getText().toString());
}
add.setOnClickListener(
new Button.OnClickListener(){
public void onClick(View v){
logica();
x = 0;
}
}
);
sub.setOnClickListener(
new Button.OnClickListener(){
public void onClick(View v){
logics();
x = 0;
}
}
);
mul.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
logicm();
x = 0;
}
}
);
div.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
logicd();
x = 0;
}
}
);
eq.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
ans.setText(String.valueOf(a));
}
}
);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/tt"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="50dp"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:id="@+id/add"
android:layout_marginTop="73dp"
android:layout_below="@+id/tt"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="27dp"
android:layout_marginStart="27dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:id="@+id/sub"
android:layout_alignTop="@+id/add"
android:layout_toLeftOf="@+id/mul"
android:layout_toStartOf="@+id/mul"
android:layout_marginRight="42dp"
android:layout_marginEnd="42dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="x"
android:id="@+id/mul"
android:layout_marginRight="46dp"
android:layout_marginEnd="46dp"
android:layout_alignTop="@+id/sub"
android:layout_toLeftOf="@+id/div"
android:layout_toStartOf="@+id/div" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
android:id="@+id/div"
android:layout_alignTop="@+id/mul"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="25dp"
android:layout_marginEnd="25dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="="
android:id="@+id/eq"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/mul"
android:layout_toStartOf="@+id/mul" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="ans"
android:id="@+id/ans"
android:layout_below="@+id/tt"
android:layout_alignLeft="@+id/eq"
android:layout_alignStart="@+id/eq" />
</RelativeLayout>
Logcat
10-27 22:36:08.318 17077-17077/com.example.parth.editcal E/AndroidRuntime: FATAL EXCEPTION: main 10-27 22:36:08.318 17077-17077/com.example.parth.editcal E/AndroidRuntime: Process: com.example.parth.editcal, PID: 17077 10-27 22:36:08.318 17077-17077/com.example.parth.editcal E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.parth.editcal/com.example.parth.editcal.MainActivity}: java.lang.NumberFormatException: Invalid int: "" 10-27 22:36:08.318 17077-17077/com.example.parth.editcal E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2548) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2607) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal E/AndroidRuntime: at android.app.ActivityThread.access$900(ActivityThread.java:174) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1325) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal E/AndroidRuntime: at android.os.Looper.loop(Looper.java:146) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5756) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal E/AndroidRuntime: Caused by: java.lang.NumberFormatException: Invalid int: "" 10-27 22:36:08.318 17077-17077/com.example.parth.editcal E/AndroidRuntime: at java.lang.Integer.invalidInt(Integer.java:137) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal E/AndroidRuntime: at java.lang.Integer.parseInt(Integer.java:358) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal E/AndroidRuntime: at java.lang.Integer.parseInt(Integer.java:331) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal E/AndroidRuntime: at com.example.parth.editcal.MainActivity.onCreate(MainActivity.java:190) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5619) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2512) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2607) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal E/AndroidRuntime: at android.app.ActivityThread.access$900(ActivityThread.java:174) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1325) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal E/AndroidRuntime: at android.os.Looper.loop(Looper.java:146) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5756) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
你需要检查你的字符串是否为空 - 否则你无法解析它的整数:
if (x == 0 && !tt.getText().toString().isEmpty()){
x = Integer.parseInt(tt.getText().toString());
}
答案 1 :(得分:0)
您需要检查您正在解析为int的字符串值是否在下面一行中为空
if (x == 0){
if(null != tt.getText().toString() && !"".equals(tt.getText().toString().trim())){
x = Integer.parseInt(tt.getText().toString());
}
}
希望这有帮助!