0

this is the fragmentOne.java
package piestudio.opinion;

import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.MenuItem;

/**
 * Created by jene on 7/4/2016.
 */
public class FragmentOne extends Fragment{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener( new NavigationView.OnNavigationItemSelectedListener(){

            public boolean onNavigationItemSelected(MenuItem Item){
              int id = Item.getItemId();

            if( id  == R.id.toi) {
                DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
                FragmentOne FragmentOne = new FragmentOne();
                FragmentManager.beginTransaction()
                        .add(R.id.content_frame,FragmentOne,"Times of India")
                        .commit();
                drawer.closeDrawer(navigationView);
            }
                DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
                drawer.closeDrawer(GravityCompat.START);
                return true;
            }
        });
    }
}

i am getting these errors

Error:(19, 64) error: cannot find symbol method findViewById(int) Error:(27, 54) error: cannot find symbol method findViewById(int) Error:(29, 32) error: non-static method beginTransaction() cannot be referenced from a static context Error:(37, 54) error: cannot find symbol method findViewById(int)

Ben-J
  • 1,084
  • 8
  • 24
ron
  • 65
  • 2
  • 2
  • 10
  • As this class is fragment you dont have layout resource in your onCreate method. layout created after your onCreateView where you are creating view. Below is fragment life cycle.https://developer.android.com/guide/components/fragments.html#Creating – Drup Desai Jul 08 '16 at 10:07

4 Answers4

4

Fragment haven't method

findViewById

You need getView() and use that method. Check this findViewById in Fragment

So change the line to:

final NavigationView navigationView = (NavigationView) getView().findViewById(R.id.nav_view);

Plus in fragments work with view should be in/after onCreateView method of fragment life-cycle after inflating fragment's layout.

Marcel Hofgesang
  • 951
  • 1
  • 15
  • 36
  • `getView()` can be `null` in `onCreateView()`. In this case use `view.findViewById(R.id.nav_view);` (where `view = inflater.inflate(...)`). – CoolMind Jan 25 '19 at 11:54
2

You can do like this :

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    View content = view.findViewById(R.id.content);
}
Ben-J
  • 1,084
  • 8
  • 24
0

Change below line one by one

Step 1 :

 final NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);

to this

 final NavigationView navigationView = (NavigationView)getView().findViewById(R.id.nav_view);

Step 2 :

  DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

to this

 DrawerLayout drawer = (DrawerLayout)getView().findViewById(R.id.drawer_layout);

Step 3 :

Change this

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

to this

DrawerLayout drawer = (DrawerLayout)getView().findViewById(R.id.drawer_layout);

Use getView(). This will return the root view for the fragment, with this you can call findViewById().

Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
0

Do it like this,

     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ViewGroup root = (ViewGroup) inflater.inflate(R.layout.visited_task, null);

 final NavigationView navigationView = (NavigationView) root.findViewById(R.id.nav_view);

return root;
Devraj
  • 1,479
  • 1
  • 22
  • 42