0

I am tryiing to change my code to use fragments in the OnNavigationItemSelected event

package com.example.xxxxx.myapplication;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.NavUtils;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

 @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        Fragment fragment;

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

But I am getting Fragment is unresolved in my buld but in my build.gradle of my app I added

  implementation 'com.android.support:support-v4:18.0.0'
  implementation "com.android.support:appcompat-v7:18.0.+"

Which was suggested in the below so

Android Studio: Where is the Compiler Error Output Window?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
David B
  • 315
  • 3
  • 13

1 Answers1

0

First : In the build.gradle it is not recommended to use "+".

Current version for these packages are :

    implementation 'com.android.support:support-v4:27.1.1'
    implementation 'com.android.support:appcompat-v7:27.1.1'

Second :

Try to add also this line :

    implementation 'com.android.support:support-fragment:27.1.1'
Vince
  • 2,551
  • 1
  • 17
  • 22
  • That shouldn't prevent `android.app.Fragment` from being resolved – OneCricketeer Jun 03 '18 at 15:03
  • Try to not use android.app.Fragment. It is know deprecated https://stackoverflow.com/questions/20386331/android-studio-and-android-support-v4-app-fragment-cannot-resolve-symbol – Vince Jun 03 '18 at 15:07
  • But you can try this instead : https://stackoverflow.com/questions/20386331/android-studio-and-android-support-v4-app-fragment-cannot-resolve-symbol – Vince Jun 03 '18 at 15:07
  • By the way, it's not my code/question. I know it's deprecated, just pointing out that this doesn't quite answer the question – OneCricketeer Jun 03 '18 at 15:12