android.content.res.Resources $ NotFoundExceptiona

2019-05-14 08:57发布

@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

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

Answer 1:

这里是你的错误:

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

makeText你正试图在这里调用,是makeText这需要作为第二个参数resId 。 见这里获取更多信息。 既然你要打印的计数值,你必须把它转换成一个字符串。

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


Answer 2:

该线路应该是内部onClick()compareID()

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


文章来源: android.content.res.Resources$NotFoundException