android.content.res.Resources $ NotFoundException

时间:2012-05-18 15:32:18

标签: java android error-handling

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

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

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

有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:14)

这是你的错误:

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

您尝试在此处调用的makeTextmakeTextresId的第二个参数。有关详细信息,请参阅here。由于您要打印计数值,因此必须将其转换为String。

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

答案 1 :(得分:0)

此行应位于onClick()compareID()内:

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