2012-04-06 124 views
1

我想從我的方法中獲取值並將其顯示在另一個類中作爲敬酒(只是爲了確保我的方法正常)。我得到一個空指針異常,我似乎無法弄清楚,我已經嘗試了多種不同的事情。我甚至嘗試給我的「這個」和「那個」的字符串值。然後我的日誌貓告訴我嘗試訪問類時出現空指針異常

04-05 21:17:29.633:E/AndroidRuntime(18838):java.lang.RuntimeException:無法啓動活動ComponentInfo {com.cerealBarApps/com.cerealBarApps.Testing}:android .content.res.Resources $ NotFoundException:字符串資源ID#0x0

這是我想用來運行一切的類。

public class Testing extends Activity { 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     String Testeroni = "This"; 
     String Testerhynocerous = "That"; 

     LoginTest test = new LoginTest(); 

     Toast.makeText(getApplicationContext(), (test.TestUsername(Testeroni)), 
       Toast.LENGTH_SHORT).show(); 

     Toast.makeText(getApplicationContext(), 
       (test.TestPassword(Testerhynocerous)), Toast.LENGTH_SHORT) 
       .show(); 

    } 
} 

這是我打電話來運行方法的類。

public class LoginTest { 

    // 0 = Username length is less than 4 or greater than 15 
    // 1 = Username character is not a letter/or digit 
    // 9 = Everything is okay in username :) 
    public int TestUsername(String username) { 
     if (username.length() <= 4 || username.length() >= 15) { 

      return 0; 
     } 

     for (int i = 0; i < username.length(); i++) { 
      if (Character.isDigit(username.charAt(i)) 
        || Character.isJavaLetter(username.charAt(i))) { 
       System.out.println(""); 
       // Do Nothing 
      } else 

       return 1; 
     } 
     return 9; 
    } 



     // 2 = Passowrd length is less than 4 or greater than 15 
     // 3 = Password character is not a digit 
     // 8 = Everything is okay in password :) 
     public int TestPassword(String password) { 
      if (password.length() <= 4 || password.length() >= 15) { 
       return 2; 
      } 

      for (int i = 0; i < password.length(); i++) { 
       if (Character.isDigit(password.charAt(i)) 
         || Character.isJavaLetter(password.charAt(i))) { 
        System.out.println(""); 
        // Do Nothing 
       } else 
        return 3; 
      } 
      return 8; 
     } 
    } 
+0

Android使用int數據類型作爲資源ID,因此您應該將變量 int值爲字符串值 – 2012-04-06 01:33:16

回答

1

這些代碼:

Toast.makeText(getApplicationContext(), (test.TestUsername(Testeroni)), 
      Toast.LENGTH_SHORT).show(); 

    Toast.makeText(getApplicationContext(), 
      (test.TestPassword(Testerhynocerous)), Toast.LENGTH_SHORT) 
      .show(); 

有一些問題,test.TestUsername(Testeroni)test.TestPassword(Testerhynocerous)返回類型爲int,所以它認爲這樣的字符串資源ID,因此如果能更改爲:

Toast.makeText(getApplicationContext(), (""+test.TestUsername(Testeroni)), 
      Toast.LENGTH_SHORT).show(); 

    Toast.makeText(getApplicationContext(), 
      (""+test.TestPassword(Testerhynocerous)), Toast.LENGTH_SHORT) 
      .show(); 
+0

我覺得自己很笨...謝謝 – 2012-04-06 01:31:54

1

的問題是在這裏:

Toast.makeText(getApplicationContext(), (test.TestUsername(Testeroni)), 
      Toast.LENGTH_SHORT).show(); 

的TestUsername返回一個int,它被解釋爲資源ID這當然是無效的。

將其更改爲:

Toast.makeText(getApplicationContext(), String.valueOf((test.TestUsername(Testeroni))), 
      Toast.LENGTH_SHORT).show(); 
相關問題