android.content.res.Resources$NotFoundException

4
@Override
public void onCreate(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);
    setContentView(R.layout.screenlocked);

    //Retrieve stored ID
    final String STORAGE = "Storage";  
    SharedPreferences unique = getSharedPreferences(STORAGE, 0);
    LoginID = unique.getString("identifier", "");

    //Retrieve stored phone number
    final String phoneNumber = unique.getString("PhoneNumber", "");
    phoneView = (TextView) findViewById(R.id.phone);
    phoneView.setText(phoneNumber.toString());

    //Retrieve user input
    input = (EditText) findViewById(R.id.editText1);
    userInput = input.getText().toString();

    //Set login button
    login = (Button) findViewById(R.id.login);
    login.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            compareID();
        }
    });
}

public void compareID(){

    if (userInput.equals(LoginID)){
        //phone screen unlocked
        //continue
        Toast.makeText(ScreenLockActivity.this, "Success!", Toast.LENGTH_SHORT).show();
    }
    else{
        count += 1;
        input.setText("");
        Toast.makeText(ScreenLockActivity.this, count, Toast.LENGTH_SHORT).show();
    }
}

我正在开发一个登录活动,想记录下用户尝试登录的次数,所以每次尝试登录时计数器都会增加一次... 但是当我运行该活动时,我的logcat中出现了以下错误:

android.content.res.Resources$NotFoundException: String resource ID #0x1

有没有人能帮我解决这个问题?


你尝试过在Eclipse IDE中清除项目(Project-Clean)后再试吗? - ρяσѕρєя K
2个回答

14

以下是你的错误:

Toast.makeText(ScreenLockActivity.this, count, Toast.LENGTH_SHORT).show();

你在这里尝试调用的makeText是带有第二个参数resIdmakeText。请参见此处以获取更多信息。由于你想打印计数值,所以必须将其转换为字符串。

String value = String.valueOf(count);
Toast.makeText(ScreenLockActivity.this, value, Toast.LENGTH_SHORT).show();

@blackbelt,我也遇到了同样的错误,请问您能告诉我如何找出错误所在的行吗? - AndroidOptimist
@AndroidOptimist 在堆栈跟踪中可以看到它。当错误发生时,logcat 中会出现很多红色行,您应该寻找指向您代码的那一行。 - Blackbelt

0

这行代码应该放在 onClick() 或者 compareID() 函数内:

userInput = input.getText().toString();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接