【问题标题】:Application is Crashing when EditText View is Left Empty当 EditText 视图为空时应用程序崩溃
【发布时间】:2013-01-20 06:40:13
【问题描述】:

我为 Name 、 body weight 和 height 创建了三个编辑文本视图 姓名采用 PersonName 值,体重和身高采用数字值 我正在使用以下代码来检查 EditText 视图是否为空的天气。正如您在下面看到的那样,如果代码块为空,我尝试了所有方法,但是当我将 editText 视图留空时,应用程序仍然崩溃。可能会出什么问题?

if(getName.equals("") || getName.length()<=2 || getName.equals(null) || getName.trim().length()<1)
                        {
                            NameWarning.setText("Please Enter Your Name Correctly");
                        }
                        else if(heightcm.getText().toString().equals("") || heightcm.getText()==null || heightcm.getText().toString().equals(null) || heightcm.getText().toString().trim().length()<1)
                        {
                            HeightWarning.setText("Please Enter Your Height Correctly");
                        }
                        else if(getWeight.getText().toString().equals("") || getWeight.getText().toString()==null || getWeight.getText().toString().equals(null) || getWeight.getText().toString().trim().length()<1)
                        {
                            WeightWarning.setText("Please Enter Your Weight Correctly");
                        }
                        else if (getName.equals("") || heightcm.getText().toString().equals("") || getWeight.getText().toString().equals("") || getName==null || heightcm.getText().toString()==null || getWeight.getText().toString()==null || getName.equals(null) || heightcm.getText().toString().equals(null) || getWeight.getText().toString().equals(null)
                                || getName.trim().length()<1 || heightcm.getText().toString().trim().length()<1 || getWeight.getText().toString().trim().length()<1)
                        {
                            NameWarning.setText("Please Fill Your Details Correctly !!");
                            HeightWarning.setText("Please Fill Your Details Correctly !!");
                            WeightWarning.setText("Please Fill Your Details Correctly !!");
                        }

当应用程序崩溃时我正在获取此文件

// Compiled from Integer.java (version 1.5 : 49.0, super bit)
// Signature: Ljava/lang/Number;Ljava/lang/Comparable<Ljava/lang/Integer;>;
public final class java.lang.Integer extends java.lang.Number implements java.lang.Comparable {

  // Field descriptor #11 I
  public static final int MAX_VALUE = 2147483647;

  // Field descriptor #11 I
  public static final int MIN_VALUE = -2147483648;

  // Field descriptor #11 I
  public static final int SIZE = 32;

  // Field descriptor #19 Ljava/lang/Class;
  // Signature: Ljava/lang/Class<Ljava/lang/Integer;>;
  public static final java.lang.Class TYPE;

  // Method descriptor #23 (I)V
  // Stack: 3, Locals: 2
  public Integer(int value);
     0  aload_0 [this]
     1  invokespecial java.lang.Number() [1]
     4  new java.lang.RuntimeException [2]
     7  dup
     8  ldc <String "Stub!"> [3]
    10  invokespecial java.lang.RuntimeException(java.lang.String) [4]
    13  athrow
      Line numbers:
        [pc: 0, line: 6]
      Local variable table:
        [pc: 0, pc: 14] local: this index: 0 type: java.lang.Integer
        [pc: 0, pc: 14] local: value index: 1 type: int

  // Method descriptor #30 (Ljava/lang/String;)V
  // Stack: 3, Locals: 2
  public Integer(java.lang.String string) throws java.lang.NumberFormatException;
     0  aload_0 [this]
     1  invokespecial java.lang.Number() [1]
     4  new java.lang.RuntimeException [2]
     7  dup
     8  ldc <String "Stub!"> [3]
    10  invokespecial java.lang.RuntimeException(java.lang.String) [4]
    13  athrow
      Line numbers:
        [pc: 0, line: 7]
      Local variable table:
        [pc: 0, pc: 14] local: this index: 0 type: java.lang.Integer
        [pc: 0, pc: 14] local: string index: 1 type: java.lang.String

  // Method descriptor #36 ()B
  // Stack: 3, Locals: 1
  public byte byteValue();
     0  new java.lang.RuntimeException [2]
     3  dup
     4  ldc <String "Stub!"> [3]
     6  invokespecial java.lang.RuntimeException(java.lang.String) [4]
     9  athrow
      Line numbers:
        [pc: 0, line: 8]
      Local variable table:
        [pc: 0, pc: 10] local: this index: 0 type: java.lang.Integer

  // Method descriptor #38 (Ljava/lang/Integer;)I
  // Stack: 3, Locals: 2
  public int compareTo(java.lang.Integer object);
     0  new java.lang.RuntimeException [2]
     3  dup
     4  ldc <String "Stub!"> [3]
     6  invokespecial java.lang.RuntimeException(java.lang.String) [4]
     9  athrow
      Line numbers:
        [pc: 0, line: 9]
      Local variable table:
        [pc: 0, pc: 10] local: this index: 0 type: java.lang.Integer
        [pc: 0, pc: 10] local: object index: 1 type: java.lang.Integer

  // Method descriptor #41 (Ljava/lang/String;)Ljava/lang/Integer;
  // Stack: 3, Locals: 1
  public static java.lang.Integer decode(java.lang.String string) throws java.lang.NumberFormatException;
     0  new java.lang.RuntimeException [2]
     3  dup
     4  ldc <String "Stub!"> [3]
     6  invokespecial java.lang.RuntimeException(java.lang.String) [4]
     9  athrow
      Line numbers:
        [pc: 0, line: 10]
      Local variable table:
        [pc: 0, pc: 10] local: string index: 0 type: java.lang.String

  // Method descriptor #43 ()D
  // Stack: 3, Locals: 1
  public double doubleValue();
     0  new java.lang.RuntimeException [2]
     3  dup
     4  ldc <String "Stub!"> [3]
     6  invokespecial java.lang.RuntimeException(java.lang.String) [4]
     9  athrow
      Line numbers:
        [pc: 0, line: 11]
      Local variable table:
        [pc: 0, pc: 10] local: this index: 0 type: java.lang.Integer

  // Method descriptor #45 (Ljava/lang/Object;)Z
  // Stack: 3, Locals: 2
  public boolean equals(java.lang.Object o);
     0  new java.lang.RuntimeException [2]
     3  dup
     4  ldc <String "Stub!"> [3]
     6  invokespecial java.lang.RuntimeException(java.lang.String) [4]
     9  athrow
      Line numbers:
        [pc: 0, line: 12]
      Local variable table:
        [pc: 0, pc: 10] local: this index: 0 type: java.lang.Integer
        [pc: 0, pc: 10] local: o index: 1 type: java.lang.Object

  // Method descriptor #49 ()F
  // Stack: 3, Locals: 1
  public float floatValue();
     0  new java.lang.RuntimeException [2]
     3  dup
     4  ldc <String "Stub!"> [3]
     6  invokespecial java.lang.RuntimeException(java.lang.String) [4]
     9  athrow
      Line numbers:
        [pc: 0, line: 13]
      Local variable table:
        [pc: 0, pc: 10] local: this index: 0 type: java.lang.Integer

  // Method descriptor #41 (Ljava/lang/String;)Ljava/lang/Integer;
  // Stack: 3, Locals: 1
  public static java.lang.Integer getInteger(java.lang.String string);
     0  new java.lang.RuntimeException [2]
     3  dup
     4  ldc <String "Stub!"> [3]
     6  invokespecial java.lang.RuntimeException(java.lang.String) [4]
     9  athrow
      Line numbers:
        [pc: 0, line: 14]
      Local variable table:
        [pc: 0, pc: 10] local: string index: 0 type: java.lang.String

  // Method descriptor #51 (Ljava/lang/String;I)Ljava/lang/Integer;
  // Stack: 3, Locals: 2
  public static java.lang.Integer getInteger(java.lang.String string, int defaultValue);
     0  new java.lang.RuntimeException [2]
     3  dup
     4  ldc <String "Stub!"> [3]
     6  invokespecial java.lang.RuntimeException(java.lang.String) [4]
     9  athrow
      Line numbers:
        [pc: 0, line: 15]
      Local variable table:
        [pc: 0, pc: 10] local: string index: 0 type: java.lang.String
        [pc: 0, pc: 10] local: defaultValue index: 1 type: int

  // Method descriptor #53 (Ljava/lang/String;Ljava/lang/Integer;)Ljava/lang/Integer;
  // Stack: 3, Locals: 2
  public static java.lang.Integer getInteger(java.lang.String string, java.lang.Integer defaultValue);
     0  new java.lang.RuntimeException [2]
     3  dup
     4  ldc <String "Stub!"> [3]
     6  invokespecial java.lang.RuntimeException(java.lang.String) [4]
     9  athrow
      Line numbers:
        [pc: 0, line: 16]
      Local variable table:
        [pc: 0, pc: 10] local: string index: 0 type: java.lang.String
        [pc: 0, pc: 10] local: defaultValue index: 1 type: java.lang.Integer

  // Method descriptor #55 ()I
  // Stack: 3, Locals: 1
  public int hashCode();
     0  new java.lang.RuntimeException [2]
     3  dup
     4  ldc <String "Stub!"> [3]
     6  invokespecial java.lang.RuntimeException(java.lang.String) [4]
     9  athrow
      Line numbers:
        [pc: 0, line: 17]
      Local variable table:
        [pc: 0, pc: 10] local: this index: 0 type: java.lang.Integer

  // Method descriptor #55 ()I
  // Stack: 3, Locals: 1
  public int intValue();
     0  new java.lang.RuntimeException [2]
     3  dup
     4  ldc <String "Stub!"> [3]
     6  invokespecial java.lang.RuntimeException(java.lang.String) [4]
     9  athrow
      Line numbers:
        [pc: 0, line: 18]
      Local variable table:
        [pc: 0, pc: 10] local: this index: 0 type: java.lang.Integer

  // Method descriptor #58 ()J
  // Stack: 3, Locals: 1
  public long longValue();
     0  new java.lang.RuntimeException [2]
     3  dup
     4  ldc <String "Stub!"> [3]
     6  invokespecial java.lang.RuntimeException(java.lang.String) [4]
     9  athrow
      Line numbers:
        [pc: 0, line: 19]
      Local variable table:
        [pc: 0, pc: 10] local: this index: 0 type: java.lang.Integer

  // Method descriptor #60 (Ljava/lang/String;)I
  // Stack: 3, Locals: 1
  public static int parseInt(java.lang.String string) throws java.lang.NumberFormatException;
     0  new java.lang.RuntimeException [2]
     3  dup
     4  ldc <String "Stub!"> [3]
     6  invokespecial java.lang.RuntimeException(java.lang.String) [4]
     9  athrow
      Line numbers:
        [pc: 0, line: 20]
      Local variable table:
        [pc: 0, pc: 10] local: string index: 0 type: java.lang.String

  // Method descriptor #61 (Ljava/lang/String;I)I
  // Stack: 3, Locals: 2
  public static int parseInt(java.lang.String string, int radix) throws java.lang.NumberFormatException;
     0  new java.lang.RuntimeException [2]
     3  dup
     4  ldc <String "Stub!"> [3]
     6  invokespecial java.lang.RuntimeException(java.lang.String) [4]
     9  athrow
      Line numbers:
        [pc: 0, line: 21]
      Local variable table:
        [pc: 0, pc: 10] local: string index: 0 type: java.lang.String
        [pc: 0, pc: 10] local: radix index: 1 type: int

  // Method descriptor #64 ()S
  // Stack: 3, Locals: 1
  public short shortValue();
     0  new java.lang.RuntimeException [2]
     3  dup
     4  ldc <String "Stub!"> [3]
     6  invokespecial java.lang.RuntimeException(java.lang.String) [4]
     9  athrow
      Line numbers:
        [pc: 0, line: 22]
      Local variable table:
        [pc: 0, pc: 10] local: this index: 0 type: java.lang.Integer

  // Method descriptor #66 (I)Ljava/lang/String;
  // Stack: 3, Locals: 1
  public static java.lang.String toBinaryString(int i);
     0  new java.lang.RuntimeException [2]
     3  dup
     4  ldc <String "Stub!"> [3]
     6  invokespecial java.lang.RuntimeException(java.lang.String) [4]
     9  athrow
      Line numbers:
        [pc: 0, line: 23]
      Local variable table:
        [pc: 0, pc: 10] local: i index: 0 type: int

  // Method descriptor #66 (I)Ljava/lang/String;
  // Stack: 3, Locals: 1
  public static java.lang.String toHexString(int i);
     0  new java.lang.RuntimeException [2]
     3  dup
     4  ldc <String "Stub!"> [3]
     6  invokespecial java.lang.RuntimeException(java.lang.String) [4]
     9  athrow
      Line numbers:
        [pc: 0, line: 24]
      Local variable table:
        [pc: 0, pc: 10] local: i index: 0 type: int

  // Method descriptor #66 (I)Ljava/lang/String;
  // Stack: 3, Locals: 1
  public static java.lang.String toOctalString(int i);
     0  new java.lang.RuntimeException [2]
     3  dup
     4  ldc <String "Stub!"> [3]
     6  invokespecial java.lang.RuntimeException(java.lang.String) [4]
     9  athrow
      Line numbers:
        [pc: 0, line: 25]
      Local variable table:
        [pc: 0, pc: 10] local: i index: 0 type: int

  // Method descriptor #71 ()Ljava/lang/String;
  // Stack: 3, Locals: 1
  public java.lang.String toString();
     0  new java.lang.RuntimeException [2]
     3  dup
     4  ldc <String "Stub!"> [3]
     6  invokespecial java.lang.RuntimeException(java.lang.String) [4]
     9  athrow
      Line numbers:
        [pc: 0, line: 26]
      Local variable table:
        [pc: 0, pc: 10] local: this index: 0 type: java.lang.Integer

  // Method descriptor #66 (I)Ljava/lang/String;
  // Stack: 3, Locals: 1
  public static java.lang.String toString(int i);
     0  new java.lang.RuntimeException [2]
     3  dup
     4  ldc <String "Stub!"> [3]
     6  invokespecial java.lang.RuntimeException(java.lang.String) [4]
     9  athrow
      Line numbers:
        [pc: 0, line: 27]
      Local variable table:
        [pc: 0, pc: 10] local: i index: 0 type: int

  // Method descriptor #72 (II)Ljava/lang/String;
  // Stack: 3, Locals: 2
  public static java.lang.String toString(int i, int radix);
     0  new java.lang.RuntimeException [2]
     3  dup
     4  ldc <String "Stub!"> [3]
     6  invokespecial java.lang.RuntimeException(java.lang.String) [4]
     9  athrow
      Line numbers:
        [pc: 0, line: 28]
      Local variable table:
        [pc: 0, pc: 10] local: i index: 0 type: int
        [pc: 0, pc: 10] local: radix index: 1 type: int

  // Method descriptor #41 (Ljava/lang/String;)Ljava/lang/Integer;
  // Stack: 3, Locals: 1
  public static java.lang.Integer valueOf(java.lang.String string) throws java.lang.NumberFormatException;
     0  new java.lang.RuntimeException [2]
     3  dup
     4  ldc <String "Stub!"> [3]
     6  invokespecial java.lang.RuntimeException(java.lang.String) [4]
     9  athrow
      Line numbers:
        [pc: 0, line: 29]
      Local variable table:
        [pc: 0, pc: 10] local: string index: 0 type: java.lang.String

  // Method descriptor #51 (Ljava/lang/String;I)Ljava/lang/Integer;
  // Stack: 3, Locals: 2
  public static java.lang.Integer valueOf(java.lang.String string, int radix) throws java.lang.NumberFormatException;
     0  new java.lang.RuntimeException [2]
     3  dup
     4  ldc <String "Stub!"> [3]
     6  invokespecial java.lang.RuntimeException(java.lang.String) [4]
     9  athrow
      Line numbers:
        [pc: 0, line: 30]
      Local variable table:
        [pc: 0, pc: 10] local: string index: 0 type: java.lang.String
        [pc: 0, pc: 10] local: radix index: 1 type: int

  // Method descriptor #75 (I)I
  // Stack: 3, Locals: 1
  public static int highestOneBit(int i);
     0  new java.lang.RuntimeException [2]
     3  dup
     4  ldc <String "Stub!"> [3]
     6  invokespecial java.lang.RuntimeException(java.lang.String) [4]
     9  athrow
      Line numbers:
        [pc: 0, line: 31]
      Local variable table:
        [pc: 0, pc: 10] local: i index: 0 type: int

  // Method descriptor #75 (I)I
  // Stack: 3, Locals: 1
  public static int lowestOneBit(int i);
     0  new java.lang.RuntimeException [2]
     3  dup
     4  ldc <String "Stub!"> [3]
     6  invokespecial java.lang.RuntimeException(java.lang.String) [4]
     9  athrow
      Line numbers:
        [pc: 0, line: 32]
      Local variable table:
        [pc: 0, pc: 10] local: i index: 0 type: int

  // Method descriptor #75 (I)I
  // Stack: 3, Locals: 1
  public static int numberOfLeadingZeros(int i);
     0  new java.lang.RuntimeException [2]
     3  dup
     4  ldc <String "Stub!"> [3]
     6  invokespecial java.lang.RuntimeException(java.lang.String) [4]
     9  athrow
      Line numbers:
        [pc: 0, line: 33]
      Local variable table:
        [pc: 0, pc: 10] local: i index: 0 type: int

  // Method descriptor #75 (I)I
  // Stack: 3, Locals: 1
  public static int numberOfTrailingZeros(int i);
     0  new java.lang.RuntimeException [2]
     3  dup
     4  ldc <String "Stub!"> [3]
     6  invokespecial java.lang.RuntimeException(java.lang.String) [4]
     9  athrow
      Line numbers:
        [pc: 0, line: 34]
      Local variable table:
        [pc: 0, pc: 10] local: i index: 0 type: int

  // Method descriptor #75 (I)I
  // Stack: 3, Locals: 1
  public static int bitCount(int i);
     0  new java.lang.RuntimeException [2]
     3  dup
     4  ldc <String "Stub!"> [3]
     6  invokespecial java.lang.RuntimeException(java.lang.String) [4]
     9  athrow
      Line numbers:
        [pc: 0, line: 35]
      Local variable table:
        [pc: 0, pc: 10] local: i index: 0 type: int

  // Method descriptor #81 (II)I
  // Stack: 3, Locals: 2
  public static int rotateLeft(int i, int distance);
     0  new java.lang.RuntimeException [2]
     3  dup
     4  ldc <String "Stub!"> [3]
     6  invokespecial java.lang.RuntimeException(java.lang.String) [4]
     9  athrow
      Line numbers:
        [pc: 0, line: 36]
      Local variable table:
        [pc: 0, pc: 10] local: i index: 0 type: int
        [pc: 0, pc: 10] local: distance index: 1 type: int

  // Method descriptor #81 (II)I
  // Stack: 3, Locals: 2
  public static int rotateRight(int i, int distance);
     0  new java.lang.RuntimeException [2]
     3  dup
     4  ldc <String "Stub!"> [3]
     6  invokespecial java.lang.RuntimeException(java.lang.String) [4]
     9  athrow
      Line numbers:
        [pc: 0, line: 37]
      Local variable table:
        [pc: 0, pc: 10] local: i index: 0 type: int
        [pc: 0, pc: 10] local: distance index: 1 type: int

  // Method descriptor #75 (I)I
  // Stack: 3, Locals: 1
  public static int reverseBytes(int i);
     0  new java.lang.RuntimeException [2]
     3  dup
     4  ldc <String "Stub!"> [3]
     6  invokespecial java.lang.RuntimeException(java.lang.String) [4]
     9  athrow
      Line numbers:
        [pc: 0, line: 38]
      Local variable table:
        [pc: 0, pc: 10] local: i index: 0 type: int

  // Method descriptor #75 (I)I
  // Stack: 3, Locals: 1
  public static int reverse(int i);
     0  new java.lang.RuntimeException [2]
     3  dup
     4  ldc <String "Stub!"> [3]
     6  invokespecial java.lang.RuntimeException(java.lang.String) [4]
     9  athrow
      Line numbers:
        [pc: 0, line: 39]
      Local variable table:
        [pc: 0, pc: 10] local: i index: 0 type: int

  // Method descriptor #75 (I)I
  // Stack: 3, Locals: 1
  public static int signum(int i);
     0  new java.lang.RuntimeException [2]
     3  dup
     4  ldc <String "Stub!"> [3]
     6  invokespecial java.lang.RuntimeException(java.lang.String) [4]
     9  athrow
      Line numbers:
        [pc: 0, line: 40]
      Local variable table:
        [pc: 0, pc: 10] local: i index: 0 type: int

  // Method descriptor #87 (I)Ljava/lang/Integer;
  // Stack: 3, Locals: 1
  public static java.lang.Integer valueOf(int i);
     0  new java.lang.RuntimeException [2]
     3  dup
     4  ldc <String "Stub!"> [3]
     6  invokespecial java.lang.RuntimeException(java.lang.String) [4]
     9  athrow
      Line numbers:
        [pc: 0, line: 41]
      Local variable table:
        [pc: 0, pc: 10] local: i index: 0 type: int

  // Method descriptor #88 (Ljava/lang/Object;)I
  // Stack: 2, Locals: 2
  public bridge synthetic int compareTo(java.lang.Object x0);
    0  aload_0 [this]
    1  aload_1 [x0]
    2  checkcast java.lang.Integer [5]
    5  invokevirtual java.lang.Integer.compareTo(java.lang.Integer) : int [6]
    8  ireturn
      Line numbers:
        [pc: 0, line: 2]
      Local variable table:
        [pc: 0, pc: 9] local: this index: 0 type: java.lang.Integer
        [pc: 0, pc: 9] local: x0 index: 1 type: java.lang.Object

  // Method descriptor #91 ()V
  // Stack: 1, Locals: 0
  static {};
    0  aconst_null
    1  putstatic java.lang.Integer.TYPE : java.lang.Class [7]
    4  return
      Line numbers:
        [pc: 0, line: 47]
}

【问题讨论】:

  • “崩溃”不是对问题的有用描述。与其尝试许多 [随机] 事物,不如先确定什么问题是什么,以便确定具体的解决方案(针对实际问题);可能存在异常和相关的堆栈跟踪。使用它。
  • 另外 1. 一个有效的 toString永远返回 null,因此检查它是没有意义的。
  • 还有 2. getNames.equals("") || getName == null 很傻。它要么是错误的,要么是无效的;布尔运算符是左结合的;到应用空值检查时,已经为时已晚,无法阻止 NPE。

标签: android android-widget


【解决方案1】:

试试这个方法:

 if(getName.getText.toString().equals("") || getName.getText().toString().length()<=2 || getName.getText().toString().equals(null) || getName.getText().toString().trim().length()<1)
                    {
                        NameWarning.setText("Please Enter Your Name Correctly");
                    }
                   ..................

也使用.getText().toString() 作为您的第一个 if 条件。

【讨论】:

  • 我正在使用此代码而不是 if(getName.equals("") || getName.trim().length()
  • 但是你在哪里定义了 .getText.toString() 在你的第一行你可以检查你的代码你会得到不同的。
猜你喜欢
  • 2016-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-21
相关资源
最近更新 更多