Android: Including multiple Java Packages to Manifest

前端 未结 5 962
执笔经年
执笔经年 2020-12-05 04:22

The app I am developing has many activities organized into seven java packages. Originally I wrote all the coding and stuff for each group of activities in a java package as

相关标签:
5条回答
  • 2020-12-05 04:58

    The problem may persist even if we change the manifest file. to avoid it we must Add an import com.example.R; in all our classes.

    example: MainActivity.class in package2

    pakage com.example.package2.Activity2
    import com.example.R;
    

    (TestActivity.class in package1)

    pakage com.example.package1.Activity1
    import com.example.R;
    
    0 讨论(0)
  • 2020-12-05 05:07

    Android automatically creates the class named "R" in the package declared in your App's manifest. When all of your classes are inside that package, you'll never have to explicitly import "R". However, if you have classes in other packages, they won't see it by default and you will have to include

     import <app-package>.R;
    

    or

     import <app-package>.*;
    

    (substituting the actual name for <app-package> of course).

    If you include library projects in your App, then they can reference their own "R" classes, which will be generated within their home packages. If you have several independent activities which need to be bundled together into one final App, you should seriously consider using library projects instead of manually merging things. It could make life much easier for you.

    0 讨论(0)
  • 2020-12-05 05:09

    Make sure that the import statement at the top of the Activity references the correct R file. Each project has its own R file, so if you copy an Activity from one project to another it will still be trying to reference the R file from the old project.

    You do not need any explicit inclusion of different packages in the manifest. To include activities from two different packages, say:

    com.example.package1.Activity1
    com.example.package2.Activity2
    

    you can do the following:

    <manifest package="com.example" . . . >
      <application . . .>
        <activity android:name=".package1.Activity1" . . . />
        <activity android:name=".package2.Activity2" . . . />
      </application>
    </manifest>
    
    0 讨论(0)
  • 2020-12-05 05:16

    Check that the layout -> main.xml file is correct and includes the android:id="@+id/whateverIdHasCausedYouTheError"

    The R.java file will then be updated to include the id.. and bam, your error should disappear.

    0 讨论(0)
  • 2020-12-05 05:18

    I am using APK Builder and all xml resources are declared in an auto generated java file by aapt. Its package name is you guess, the android manifest package name.

    Then it dawned on me that xml files dont have package names. All xml files are just xml files. Then aapt generates an R class with all XML resources in one R class. The package name of this class is the one in the manifest.

    So either import package-name.R or just use one package for your entire project.

    0 讨论(0)
提交回复
热议问题