I created a fragment that is connected to XML containing a single image. The navigation box (using the action bar) is configured inside activity_main.java and works fine. What I want to do is when I click on the image inside the fragment, the navigation box should βswitchβ.
The following code inside the onCreateView method returns a null pointer:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedinstanceState) { View view=inflater.inflate(R.layout.home, container, false); ImageView img=(ImageView)view.findViewById(R.id.imageView1); // Defined inside activity_main.xml final LinearLayout mDrawer = (LinearLayout) view.findViewById(R.id.drawer); // Defined inside activity_main.xml final DrawerLayout mDrawerLayout = (DrawerLayout)view.findViewById(R.id.drawer_layout); img.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mDrawerLayout.openDrawer(mDrawer); } }); return view; }
The null pointer is returned in the following expression:
mDrawerLayout.openDrawer (mDrawer);
android android-fragments navigation-drawer android-actionbar-compat
sabergeek
source share