添加ProgressBar遇错误:CAN NOT RESOLVE SYMBOL LOADING,求代码排查帮助
Hey there, let's figure out why you're seeing that "CAN NOT RESOLVE SYMBOL LOADING" error when setting up your progress dialog.
First off, looking at your code:
public void showProgressDialog() { if (mProgressDialog == null) { mProgressDialog = new ProgressDialog(this); mProgressDialog.setMessage(getString(R.string.loading)); // 这里就是问题核心! mProgressDialog.setIndeterminate(true); } mProgressDialog.show(); } public void hideProgressDialog() { if (mProgressDialog != null && mProgressDialog.isShowing()) { mProgressDialog.dismiss(); } }
The error has nothing to do with the ProgressBar/ProgressDialog component itself — it's telling you the app can't locate a string resource named loading. Here's how to fix it step by step:
- Check your
res/values/strings.xmlfile. You either don't have a string entry namedloading, or there's a case mismatch (resource names are case-sensitive!)- If it's missing, add this line inside the
<resources>tag:
Feel free to replace "Loading..." with any text you want displayed in the progress dialog.<string name="loading">Loading...</string>
- If it's missing, add this line inside the
- If you already have the entry, try cleaning and rebuilding your project:
- In Android Studio, go to
Build > Clean Project, thenBuild > Rebuild Project. Sometimes cached resource data causes false errors like this.
- In Android Studio, go to
- Double-check the spelling: Make sure the name in
strings.xmlis exactlyloading(all lowercase, no typos) to matchR.string.loadingin your code.
A quick side note: The ProgressDialog class is deprecated in newer Android versions. For better compatibility and Material Design support, you might want to switch to using a ProgressBar inside a custom DialogFragment or the Material Components ProgressIndicator later — but let's get your current code working first!
内容的提问来源于stack exchange,提问作者valdimiro einstein

