Error 'No resource identifier found' with Android sample project

后端 未结 4 2183
暗喜
暗喜 2021-02-19 01:48

I am trying to open the Android sample project - Navigation Drawer in Android Studio, but got error while building it.

These are the steps to reproduce it:

4条回答
  •  情深已故
    2021-02-19 02:28

    The compiler cannot find this attribute in the activity_navigation_drawer.xml:

    app:layoutManager="LinearLayoutManager"
    

    To fix this

    1. Remove this attribute from the XML so your Recyclerview looks like this:

      
      
    2. press the Gradle Sync button

    3. In NavigationDrawerActivity, add the following 2 lines of code in your OnCreate();

      LinearLayoutManager mLinearLayoutmanager = new LinearLayoutManager(this);
      mDrawerList.setLayoutManager(mLinearLayoutmanager);
      

    So your code now looks like this:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation_drawer);
    
        mTitle = mDrawerTitle = getTitle();
        mPlanetTitles = getResources().getStringArray(R.array.planets_array);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (RecyclerView) findViewById(R.id.left_drawer);
    
        LinearLayoutManager mLinearLayoutmanager = new LinearLayoutManager(this);
        mDrawerList.setLayoutManager(mLinearLayoutmanager);
        ....
    

    Good luck

    Edit: The reason why this the original xml code is no longer working, is most likely that the xmlns:app="http://schemas.android.com/apk/res-auto" namespace is no longer supporting the layoutManager attribute.

    More info on the namespace can be found here: https://stackoverflow.com/a/26692768/3708094

提交回复
热议问题