why does R does not exist error come in android?

2019-01-24 04:30发布

packages/apps/Myfolder/src/com/android/myfolder/MyFile.java:196: package R does not exist
                  addPreferencesFromResource(R.xml.myfile);
                                              ^
packages/apps/Myfolder/src/com/android/myfolder/MyFile.java:344: package R does not exist
        menu.add(0, MENU_SAVE, 0, R.string.menu_save)
                                   ^
packages/apps/Myfolder/src/com/android/myfolder/MyFile.java:346: package R does not exist
        menu.add(0, MENU_CANCEL, 0, R.string.menu_cancel)
                                     ^
packages/apps/Myfolder/src/com/android/myfolder/MyFile.java:454: package R does not exist
                     errorMsg = mRes.getString(R.string.error_empty);
                                                ^
packages/apps/Myfolder/src/com/android/myfolder/MyFile.java:458: package R does not exist
                     errorMsg = mRes.getString(R.string.error_empty);

7条回答
走好不送
2楼-- · 2019-01-24 04:33

You can try import packagename.R;

查看更多
我想做一个坏孩纸
3楼-- · 2019-01-24 04:35

In my case, this error occurred because I had changed the package name of the app (before publishing to Google Play, of course), but I forgot to update the package attribute of the manifest element in my AndroidManifest.xml file. Once the package attribute agreed with the new package name, the error went away.

查看更多
一纸荒年 Trace。
4楼-- · 2019-01-24 04:37

If you are building from an ant script, you must run aapt. See the "-resource-src" target in $SDK_DIR/tools/ant/main_rules.xml.

查看更多
对你真心纯属浪费
5楼-- · 2019-01-24 04:42

Check if there are any errors in your resource files or any missing dependencies. Either of these will cause the R.java class to not be code-generated and thus a lot of errors like the ones you have shown.

查看更多
做个烂人
6楼-- · 2019-01-24 04:55

Make sure you have: package 'YOUR PACKAGE NAME' in java file that calls R class

查看更多
劫难
7楼-- · 2019-01-24 04:55

Also make sure to include your current Activity in the AndroidManifest.xml, inside the application tags. So if MyFile is your Activity subclass, you should have something like this in it:

<application 
    android:label="@string/app_name" 
    ... >

        <activity android:name=".MyFile"
            android:configChanges="orientation|keyboardHidden"
            android:label="@string/app_name">
            <intent-filter> 
                <action android:name="android.intent.action.VIEW" /> 
            </intent-filter> 
        </activity>
        ..


</application>

Although what's actually in there depends on your activity. More information about this at: http://developer.android.com/guide/topics/manifest/manifest-intro.html

查看更多
登录 后发表回答