【问题标题】:null pointer exception when trying to access class尝试访问类时出现空指针异常
【发布时间】:2012-04-06 01:24:20
【问题描述】:

我正在尝试从我的方法中获取值,并将其作为 toast 显示在另一个类中(只是为了确保我的方法有效)。我得到一个我似乎无法弄清楚的空指针异常,我尝试了多种不同的东西。我什至尝试给我的字符串值“This”和“That”。然后我的日志猫告诉我

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;
        }
    }

【问题讨论】:

  • Android 使用 int 数据类型作为资源 id,所以你应该将变量 int 值转换为 String 值

标签: java android class methods nullpointerexception


【解决方案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,所以它认为是字符串res id,所以if可以改成这样:

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

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

【讨论】:

    【解决方案2】:

    问题出在这里:

    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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-26
      • 1970-01-01
      • 1970-01-01
      • 2015-03-22
      • 2016-01-16
      • 2016-04-16
      • 2020-11-13
      • 1970-01-01
      相关资源
      最近更新 更多