【问题标题】:android.content.res.Resources$NotFoundExceptionandroid.content.res.Resources$NotFoundException
【发布时间】:2012-05-26 04:48:12
【问题描述】:
@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,

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

【问题讨论】:

  • 您在 Eclipse IDE 中从 Project-Clean 清除项目后尝试过吗?

标签: java android error-handling


【解决方案1】:

这是你的错误:

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

您试图在这里调用的makeText 是makeText,它以resId 作为第二个参数。请参阅here 了解更多信息。由于要打印计数值,因此必须将其转换为字符串。

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

【讨论】:

  • @blackbelt,我遇到了同样的错误,请告诉我如何识别错误行
  • @AndroidOptimist 在堆栈跟踪中。在 logcat 中你会看到很多红线,当错误发生时。你应该寻找在你的代码中指出的那个
【解决方案2】:

此行应在onClick() 或compareID() 内:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多