Change fragments when button is clicked - android

How to change fragments when button is clicked? I'm using android studio 1.5 and using the new navigation drawer.
This is my MainActivity.java
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;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "bryanposvoc1339.garcia#gmail.com", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
Fragment fragment;
if (id == R.id.nav_cough) {
fragment = new first();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.mainFrame, fragment);
ft.commit();
}
else if (id == R.id.nav_colds) {
fragment = new second();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.mainFrame, fragment);
ft.commit();
}
else if (id == R.id.nav_fever) {
fragment = new third();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.mainFrame, fragment);
ft.commit();
}
else if (id == R.id.nav_vegetables) {
fragment = new fourth();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.mainFrame, fragment);
ft.commit();
} else if (id == R.id.nav_fruits) {
fragment = new fifth();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.mainFrame, fragment);
ft.commit();
} else if (id == R.id.nav_about) {
fragment = new sixth();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.mainFrame, fragment);
ft.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
This is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
content_main.xml this is where my framelayout is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="appnaturemedicine.com.example.posvoc.naturesmedicine.MainActivity"
tools:showIn="#layout/app_bar_main"
android:background="#drawable/background">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" android:id="#+id/mainFrame">
</FrameLayout>
</RelativeLayout>
I am able to go to the fragment i want, the problem is that how can i make the buttons in the fragment im in at to change the current fragment when clicked
here's my first fragment when i click one of the menu in the navigationdrawer
fragment_first.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="appnaturemedicine.com.example.posvoc.naturesmedicine.first">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:id="#+id/imageView2"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#mipmap/ic_launcher_cough" />
<Button
android:layout_width="80dp"
android:layout_height="30dp"
android:id="#+id/button"
android:layout_alignBottom="#+id/textView3"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="onButtonClicked"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Lagundi (Vitex negundo)"
android:id="#+id/textView3"
android:textColor="#ff4081"
android:layout_below="#+id/imageView3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Honey"
android:id="#+id/textView4"
android:textColor="#ff4081"
android:layout_alignBottom="#+id/button3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="80dp"
android:layout_height="30dp"
android:id="#+id/button3"
android:layout_below="#+id/button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Probiotics"
android:id="#+id/textView5"
android:layout_alignBottom="#+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/button4"
android:textColor="#ff4081"
android:layout_toStartOf="#+id/button4" />
<Button
android:layout_width="80dp"
android:layout_height="30dp"
android:id="#+id/button4"
android:layout_below="#+id/textView4"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="80dp"
android:layout_height="30dp"
android:id="#+id/button5"
android:layout_below="#+id/button4"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="80dp"
android:layout_height="30dp"
android:id="#+id/button6"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="#+id/button5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Bromelain"
android:id="#+id/textView6"
android:textColor="#ff4081"
android:layout_alignBaseline="#+id/button5"
android:layout_alignBottom="#+id/button5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Peppermint"
android:id="#+id/textView7"
android:textColor="#ff4081"
android:layout_alignBaseline="#+id/button6"
android:layout_alignBottom="#+id/button6"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="250dp"
android:layout_height="110dp"
android:id="#+id/imageView3"
android:background="#drawable/ben"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
first.java
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {#link Fragment} subclass.
*/
public class first extends Fragment {
public first() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_first, container, false);
}
}
Can you make it something like when you click btn1 it goes to fragmentOne and when click btn2 it goes to fragmentTwo an if-else statement?
What do I put in here?

You can use OnfragmentInteractionListener for this. It is a interface that your activity should implement. You can call the methods from your fragments and it will be like it is calling a method from the activity.
In the following example, first fragment has a button which when clicked calls the method changeFragment(2) and similarly the second fragment calls method changeFragment(1). This method is implemented in the main activity and through if .. else it figures out which fragment to replace.
OnFragmentInteractionListener.java
public interface OnFragmentInteractionListener {
public void changeFragment(int id);
}
FragmentA:
public class first extends Fragment {
private OnFragmentInteractionListener mListener;
public first() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_first, container, false);
Button btn = (Button) view.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mListener.changeFragment(2);
}
});
return view;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
}
FragmentB:
public class first extends Fragment {
private OnFragmentInteractionListener mListener;
public first() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_first, container, false);
Button btn = (Button) view.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mListener.changeFragment(1);
}
});
return view;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
}
MainActivity:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, OnFragmentInteractionListener {
//Rest of the code
#Override
public void changeFragment(int id){
if (id == 1) {
fragment = new first();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.mainFrame, fragment);
ft.commit();
}
else if (id == 2) {
fragment = new second();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.mainFrame, fragment);
ft.commit();
}
}
}
Hope it helps!!

Related

Using AndroidStudio emulation On_Click Events within fragments firing when using tab and enter key but not mouse click

I have a problem within my code that seems to me as it is working but only in the given situation where On_Click Events within fragments are firing when using tab and enter key but not mouse click. I am using the emulator through android studio SDK23 for the emulater. When I test it on my phone sdk26 it does not allow the tap to click anything within the fragment. I feel like I may be missing something very simple.
Here is my Activity:
package com.wgu.c196.testproject;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
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;
import android.widget.TextView;
import com.wgu.c196.testproject.database.TermDatabase;
import com.wgu.c196.testproject.fragment.TestFragment;
public class MainActivity extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener,
TestFragment.OnListFragmentInteractionListener{
private DrawerLayout drawer;
private FloatingActionButton fab;
private TextView fragTitle;
public DrawerLayout getDrawer() {
return drawer;
}
public void setDrawer(DrawerLayout drawer) {
this.drawer = drawer;
}
public TextView getFragTitle() {
return fragTitle;
}
public void setFragTitle(TextView fragTitle) {
this.fragTitle = fragTitle;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Fragment testFragment = new TestFragment();
replaceFragment(testFragment );
fragTitle = findViewById(R.id.fragment_title);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
item.setChecked(true);
drawer.closeDrawers();
fab.show();
switch(item.getItemId()) {
case R.id.nav_terms:
case R.id.nav_courses:
case R.id.nav_assessment:
case R.id.nav_scheduler:
case R.id.nav_alerts:
case R.id.nav_share:
case R.id.nav_send:
default:
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
#Override
public void onDestroy(){
TermDatabase.destroyInstance();
super.onDestroy();
}
public void replaceFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
// Begin the transaction
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_main_placeholder, fragment, fragment.toString());
fragmentTransaction.addToBackStack(fragment.toString());
fragmentTransaction.commit();
}
#Override
public void onListFragmentInteraction(View view) {
}
}
My Fragment:
package com.wgu.c196.testproject.fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import com.wgu.c196.testproject.MainActivity;
import com.wgu.c196.testproject.R;
public class TestFragment extends Fragment {
// TODO: Customize parameter argument names
private static final String ARG_COLUMN_COUNT = "column-count";
// TODO: Customize parameters
private int mColumnCount = 1;
private OnListFragmentInteractionListener mListener;
private View mView;
public View getView() {
return mView;
}
public void setView(View mView) {
this.mView = mView;
}
ragment (e.g. upon screen orientation changes).
*/
public TestFragment() {
}
// TODO: Customize parameter initialization
#SuppressWarnings("unused")
public static TestFragment newInstance(int columnCount) {
TestFragment fragment = new TestFragment();
Bundle args = new Bundle();
args.putInt(ARG_COLUMN_COUNT, columnCount);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_test, container, false);
((MainActivity) mView.getContext()).getFragTitle().setText("Test Button");
Button btn = mView.findViewById(R.id.d_button_id);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Something clicked me in Fragment!", Toast.LENGTH_SHORT).show();
}
});
btn.setOnTouchListener(new View.OnTouchListener() {
#Override public boolean onTouch(View v, MotionEvent event) {
Toast.makeText(v.getContext(), "Something touched me in Fragment!", Toast.LENGTH_SHORT).show();
return false;
}
});
return mView;
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnListFragmentInteractionListener) {
mListener = (OnListFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnListFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
#Override
public void onDestroy(){
super.onDestroy();
}
public interface OnListFragmentInteractionListener {
// TODO: Update argument type and name
void onListFragmentInteraction(View view);
}
}
My main layout activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="#dimen/top_margin"
>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay"
/>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/global_top_margin"
android:theme="#style/AppTheme.AppBarOverlay">
<TextView
android:id="#+id/fragment_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:textSize="14sp"
android:textStyle="bold" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/content_main_placeholder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/main_fragment_top_margin"
android:clickable="true"
android:scrollbars="vertical"
></FrameLayout>
<include layout="#layout/nav_menu_main" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#drawable/ic_action_add_entity"/>
</android.support.design.widget.CoordinatorLayout>
My fragment layout fragment_test.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
android:id="#+id/term_list_layout"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:clickable="true">
<Button
android:id="#+id/d_button_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:text="My Button to Click"
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"
android:textAppearance="?android:textAppearanceLarge" />
</LinearLayout>
Ultimately I fixed my own problem. Which was started by choosing Navigation Activity when first starting my project.
I removed
<include layout="#layout/nav_menu_main" />
from my activity_main.xml then restructured it whith the xml from nav_menu_main, using DrawerLayout as the Parent and CoordinatorLayout from activity_main.xml as the child.
I consulted this stack Overflow which lead me to my fix.
Drawer Layout Overlapping | Covering complete space in Fragment
My new activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|enterAlways"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="#dimen/top_margin"
>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay"
/>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/global_top_margin"
android:theme="#style/AppTheme.AppBarOverlay">
<TextView
android:id="#+id/fragment_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:textSize="14sp"
android:textStyle="bold" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/content_main_placeholder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/main_fragment_top_margin"
android:clickable="true"
android:scrollbars="vertical"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#drawable/ic_action_add_entity"/>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>

Unable to Get Navigation Drawer Items to Display Items Correctly

I have got a working Navigation drawer in a blank project, however whenever I try to implement the navigation drawer in to an existing project, it doesn't seem to be working properly.
The navigation drawer displays fine, however, when items inside are selected, it doesn't seem to redirect to the fragment selected.
I have tried using many resources, however, I am still unable to progress.
Thanks in advance.
Here is my code below:
NavigationDrawer Class
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
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;
import android.view.View;
import android.widget.AbsListView;
public class NDrawer extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener,
About.OnFragmentInteractionListener, Home.OnFragmentInteractionListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Fragment fragment;
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
Fragment fragment;
if (id == R.id.nav_home) {
fragment = new Home();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.detach(fragment);
ft.attach(fragment);
ft.commit();
} else if (id == R.id.nav_rooms) {
} else if (id == R.id.nav_account) {
} else if (id == R.id.nav_settings) {
} else if (id == R.id.nav_help) {
} else if (id == R.id.nav_about) {
fragment = new About();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.detach(fragment);
ft.attach(fragment);
ft.commit();
} else if (id == R.id.nav_signout) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
#Override
public void onFragmentInteraction(Uri uri) {
}
}
About Fragment Class
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {#link Fragment} subclass.
* Activities that contain this fragment must implement the
* {#link About.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {#link About#newInstance} factory method to
* create an instance of this fragment.
*/
public class About extends Fragment {
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public About() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* #param param1 Parameter 1.
* #param param2 Parameter 2.
* #return A new instance of fragment About.
*/
public static About newInstance(String param1, String param2) {
About fragment = new About();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.activity_about, container, false);
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
tools:context=".UserActivity">
<TextView
style="#style/Base.TextAppearance.AppCompat.Headline"
android:id="#+id/displayUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="5dp"/>
<Button
android:id="#+id/listen_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Listen"
android:onClick="buttonClicked"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/textView"
/>
<TextView
android:id="#+id/status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
<TextView
style="#style/Base.TextAppearance.AppCompat.Headline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="#string/signed_in_header" />
<Button android:id="#+id/btn_settings"
android:text="#string/settings"
android:layout_marginTop="40dp"
android:layout_width="190dp"
android:layout_height="45dp"
android:onClick="buttonClicked" />
<Button android:id="#+id/btn_sign_out"
android:text="#string/sign_out"
android:layout_marginTop="40dp"
android:layout_width="190dp"
android:layout_height="45dp"
android:onClick="buttonClicked" />
<TextView
android:id="#+id/displayUsername2"
android:layout_width="242dp"
android:layout_height="34dp"
android:padding="5dp"
android:layout_margin="5dp"/>
</LinearLayout>
<include
layout="#layout/app_bar_nav"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_nav"
app:menu="#menu/activity_nav_drawer" />
</android.support.v4.widget.DrawerLayout>
activity_nav_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
+6 <group android:checkableBehavior="single">
<item
android:id="#+id/nav_home"
android:icon="#drawable/ic_home"
android:checked="true"
android:title="Home" />
<item
android:id="#+id/nav_rooms"
android:icon="#drawable/ic_room_black"
android:title="All Rooms" />
<item
android:id="#+id/nav_account"
android:icon="#drawable/ic_account_box_black"
android:title="Account" />
<item
android:id="#+id/nav_settings"
android:icon="#drawable/ic_settings_black"
android:title="Settings" />
<item
android:id="#+id/nav_help"
android:icon="#drawable/ic_help_black"
android:title="Help" />
<item
android:id="#+id/nav_about"
android:title="About" />
<item
android:id="#+id/nav_signout"
android:icon="#drawable/ic_input_black"
android:title="Logout" />
</group>
</menu>
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".NDrawer"
tools:showIn="#layout/app_bar_nav">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/content_frame"
android:orientation="horizontal" />
</android.support.constraint.ConstraintLayout>
app_bar_nav.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="aviraj.firebaseapp.NDrawer">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
But in all honesty - have one more try at this and implement the navigation drawer class from scratch from a template class and put our other main clases in method by method to see any possible conflicts.

One layout works but other doesnt

everyone. I was creating a simple app for fun and came up with this problem. Basically, I checked everything and found some problem with the layouts. I am using navigation and from navigation, it takes the user to 4 different parts. Everything works except for the last part, the button is not taking me to anywhere.
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentTransaction;
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;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout)
findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView)
findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
return super.onOptionsItemSelected(item);
}
private void displaySelectedScreen(int id)
{
Fragment fragment = null;
switch(id){
case R.id.syllabus:
fragment = new Syllabus();
break;
case R.id.questionss:
fragment = new Questions();
break;
case R.id.definitions:
fragment = new Definitions();
break;
case R.id.contact_us:
fragment = new Contact_Us();
break;
}
if(fragment != null){
FragmentTransaction ft =
getSupportFragmentManager().beginTransaction();
ft.replace(R.id.list_content,fragment).addToBackStack("tag");
ft.commit();
}
}
public void chaptersdefinitions(View view) {
Fragment newfragment = null;
if (view == findViewById(R.id.chap1def)) {
newfragment = new Chapter1def();
} else if (view == findViewById(R.id.chap2def)) {
newfragment = new Chapter2def();
} else if (view == findViewById(R.id.chap3def)) {
newfragment = new Chapter3def();
} else if (view == findViewById(R.id.chap4def)) {
newfragment = new Chapter4def();
} else if (view == findViewById(R.id.chap5def)) {
newfragment = new Chapter5def();
} else if (view == findViewById(R.id.chap6def)) {
newfragment = new Chapter6def();
} else if (view == findViewById(R.id.chap7def)) {
newfragment = new Chapter7def();
} else if (view == findViewById(R.id.chap8def)) {
newfragment = new Chapter8def();
} else if (view == findViewById(R.id.chap9def)) {
newfragment = new Chapter9def();
} else if (view == findViewById(R.id.chap10def)) {
newfragment = new Chapter10def();
} else if (view == findViewById(R.id.chapone)) {
newfragment = new Chapter1();
} else if (view == findViewById(R.id.chaptwo)) {
newfragment = new Chapter2();
} else if (view == findViewById(R.id.chapthree)) {
newfragment = new Chapter3();
} else if (view == findViewById(R.id.chapfour)) {
newfragment = new Chapter4();
} else if (view == findViewById(R.id.chapfive)) {
newfragment = new Chapter5();
} else if (view == findViewById(R.id.chapsix)) {
newfragment = new Chapter6();
} else if (view == findViewById(R.id.chapseven)) {
newfragment = new Chapter7();
} else if (view == findViewById(R.id.chapeight)) {
newfragment = new Chapter8();
} else if (view == findViewById(R.id.chapnine)) {
newfragment = new Chapter9();
} else if (view == findViewById(R.id.chapten)) {
newfragment = new Chapter10();
}
if (newfragment != null) {
FragmentTransaction ft =
getSupportFragmentManager().beginTransaction();
ft.replace(R.id.list_content,
newfragment).addToBackStack("tag");
ft.commit();
}
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
DrawerLayout drawer = (DrawerLayout)
findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
displaySelectedScreen(id);
return true;
}
}
My content_main is:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.weebly.dunyaibdp.chemistryforib.MainActivity"
tools:showIn="#layout/app_bar_main"
android:fitsSystemWindows="true">
<android.support.constraint.ConstraintLayout
android:id="#+id/list_content"
android:layout_width="451dp"
android:layout_height="600dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.885"
app:layout_constraintHorizontal_bias="0.493">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:text="Hey There!"
android:textColor="#64dd17"
android:textSize="50sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.237" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="Welcome to our Chemistry App"
android:textColor="#android:color/holo_green_dark"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.413"
app:layout_constraintHorizontal_bias="0.483" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:fontFamily="monospace"
android:text="Use navigation on the left to navigate through app"
android:textColor="#android:color/holo_green_dark"
android:textSize="12sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.558"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.53" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
In navigation i have syllabus class which is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:id="#+id/scrollSyllabus"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="UselessParent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/chapone"
android:layout_width="340sp"
android:layout_height="70sp"
android:layout_gravity="center"
android:layout_marginLeft="55sp"
android:layout_marginTop="50sp"
android:background="#drawable/roundshapebtn"
android:onClick="chaptersdefinitions"
android:text="#string/stoichometric_relations1"
android:textColor="#000000"
tools:ignore="RtlHardcoded" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Then my class for syllabus is for layout is:
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Chapter1 extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup
container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.chapteronesyll,container,false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getActivity().setTitle("Stoichometric Relations");
}
}
With this class i use this layout to show:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="one" />
</LinearLayout>
Now for this button i havent put any onclick so i just want this button to be displayed in screen and thats it. I tried some other layouts in my activity and some of them worked perfectly but some didnt i tried to delete and add again but this didnt work it still didnt print anything on the screen. Any help would be appreciated.

Android Navigation Drawer with sliding TabView

I stumbled into a crazy little 'bug', or i'm just doing something wrong. I am trying to get the swipe tab view in main activity, swipe tabView perfectly working but the main problem is when click on Navigation Drawer Menu then the fragment are overlaying each other. please Help..
here is my two tabView Fragment
1.NewsFragment
public class NewsFragment extends Fragment {
public static NewsFragment newInstance(){
NewsFragment newsFragment=new NewsFragment();
return newsFragment;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.news_fragment,null);
}}
2.NoticeFragment
public class NoticeFragment extends Fragment {
public static NoticeFragment newInstance(){
NoticeFragment noticeFragment=new NoticeFragment();
return noticeFragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.notice_fragment, container, false);
}}
Here is my two xml for two fragment
1.News_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="120dp"
android:textSize="30sp"
android:gravity="center"
android:id="#+id/textView"
android:layout_centerHorizontal="true"
android:textColor="#android:color/holo_blue_dark"
android:text="News\nFragment"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="web"
android:textSize="15sp"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:text="Android Sikkha"
android:textColor="#000"
android:layout_below="#+id/textView"
android:textStyle="italic"/>
</RelativeLayout>
2.notice_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="120dp"
android:textSize="30sp"
android:gravity="center"
android:id="#+id/textView"
android:layout_centerHorizontal="true"
android:textColor="#android:color/holo_blue_dark"
android:text="Notice\nFragment"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="web"
android:textSize="15sp"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:text="Android Sikkha"
android:textColor="#000"
android:layout_below="#+id/textView"
android:textStyle="italic"/>
</RelativeLayout>
i have another java file called Home which is same as previous one java file and also xml is same.
Here is the MainActivity
package com.example.user.navwithtab;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
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;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawer;
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Setear adaptador al viewpager.
mViewPager = (ViewPager) findViewById(R.id.pager);
setupViewPager(mViewPager);
// Preparar las pestañas
TabLayout tabs = (TabLayout) findViewById(R.id.tabs);
tabs.setupWithViewPager(mViewPager);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
private void setupViewPager(ViewPager viewPager) {
SectionsPagerAdapter adapter = new SectionsPagerAdapter(getSupportFragmentManager());
adapter.addFragment(NewsFragment.newInstance(), "News");
adapter.addFragment(NoticeFragment.newInstance(), "Notice");
viewPager.setAdapter(adapter);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragments = new ArrayList<>();
private final List<String> mFragmentTitles = new ArrayList<>();
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
#Override
public int getCount() {
return mFragments.size();
}
public void addFragment(Fragment fragment, String title) {
mFragments.add(fragment);
mFragmentTitles.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitles.get(position);
}
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
android.app.FragmentManager fragmentManager=getFragmentManager();
if (id == R.id.home) {
fragmentManager.beginTransaction().replace(R.id.content_frame,new HomeFragment()).commit();
} else if (id == R.id.announcement) {
fragmentManager.beginTransaction().replace(R.id.content_frame,new AnnouncementFragment()).commit();
} 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;
}}
here is activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
Sorry for long post.
Note: Navigation Drawer and swipe View working perfectly but the problem is on fragment overlay.after clicking navigationbarmenu
Here is my logcat
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.user.navwithtab, PID: 3400
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.widget.DrawerLayout.closeDrawer(int)' on a null object reference
at com.example.user.navwithtab.MainActivity.onNavigationItemSelected(MainActivity.java:157)
at android.support.design.widget.NavigationView$1.onMenuItemSelected(NavigationView.java:153)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:810)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:957)
at android.support.design.internal.NavigationMenuPresenter$1.onClick(NavigationMenuPresenter.java:328)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
In onNavigationItemSelected method do not create the DrawerLayout again. Simply call the closeDrawer.
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); --> remove this line of code
drawer.closeDrawer(GravityCompat.START);
UPDATE
In your onCreate method change this line of code
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
to
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
and add this line of code
DrawerLayout drawer
below this line of code
ViewPager mViewPager;
and remove the indicated line of code in onNavigationItemSelected method
if (id == R.id.home) {
fragmentManager.beginTransaction().replace(R.id.content_frame,new HomeFragment()).commit();
} else if (id == R.id.announcement) {
fragmentManager.beginTransaction().replace(R.id.content_frame,new AnnouncementFragment()).commit();
update above code as below :
if (id == R.id.home) {
fragmentManager.beginTransaction().replace(R.id.content_frame,new HomeFragment()).addToBackStack(null).commit();
} else if (id == R.id.announcement) {
fragmentManager.beginTransaction().replace(R.id.content_frame,new AnnouncementFragment()).addToBackStack(null).commit();

Opening a fragment with a button

I need some help. I've done some extensive searching for the past 4 hours and I'm not sure if it's because I have errors in my code, or if I'm not doing something correctly.
Basically, this is what's going on. I have my main activity 'homepage' which opens to a second activity with a navigation drawer. I assume that the navigation drawer is also an 'activity'. I am trying to open a fragment within that activity with the button, but I keep getting a fatal exception:
10-14 21:36:54.469 799-799/com.example.workstation.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.workstation.app/com.example.workstation.app.navigation_drawer}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.workstation.app.navigationdrawer.onCreate(navigationdrawer.java:57)
Here is the code I am working with, this one is for the navigation_drawer activity (I believe):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.workstation.app.navigation_drawer_activity$PlaceholderFragment">
<TextView
android:id="#+id/cctr_ct_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cctr_ct"
android:textSize="40sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/cctr_search"
android:drawableTop="#drawable/search"
android:layout_below="#+id/cctr_ct_home"
android:layout_alignLeft="#+id/cctr_ct_home"
android:layout_alignStart="#+id/cctr_ct_home"
android:layout_marginTop="95dp"
android:text="Search"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/cctr_favorites"
android:drawableTop="#drawable/favorites"
android:layout_alignTop="#+id/cctr_search"
android:layout_alignRight="#+id/cctr_ct_home"
android:layout_alignEnd="#+id/cctr_ct_home"
android:text="Favorite"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/cctr_recent"
android:drawableTop="#drawable/recent"
android:layout_below="#+id/cctr_search"
android:layout_alignLeft="#+id/cctr_search"
android:layout_alignStart="#+id/cctr_search"
android:layout_marginTop="37dp"
android:text="Recent"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/cctr_contactus"
android:drawableTop="#drawable/email"
android:layout_alignTop="#+id/cctr_recent"
android:layout_alignLeft="#+id/cctr_favorites"
android:layout_alignStart="#+id/cctr_favorites"
android:text="Contact Us" />
<fragment android:name="com.example.workstation.cctrmobileapp.cctrct_search"
android:id="#+id/cctr_ct_search"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</RelativeLayout>
Now, the navigation drawer works fine, it's opening the fragment from the button I'm having trouble with. I get the error above. Here is the java code from the activity that comes with the navigation drawer:
package com.example.workstation.cctrmobileapp;
import android.app.Activity;
import android.support.v4.app.FragmentTransaction;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.widget.DrawerLayout;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
public class cctr_clinical_trials extends ActionBarActivity implements NavigationDrawerFragment.NavigationDrawerCallbacks, cctrct_search.OnFragmentInteractionListener {
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in {#link #restoreActionBar()}.
*/
private CharSequence mTitle;
Button cctrct_searchButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cctr_clinical_trials);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
cctrct_searchButton = (Button)findViewById(R.id.cctr_search);
cctrct_searchButton.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View view) {
cctrct_search newFragment = new cctrct_search();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
/* cctrct_searchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentManager searchFragment = getSupportFragmentManager();
searchFragment.beginTransaction()
.replace(R.id.container, cctrct_search.newInstance("test1", "test2"))
.commit();
}
});*/
}
and for the heck of it, here is the xml for the fragment that I want to appear after I press the button:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.workstation.cctrmobileapp.cctrct_search">
<!-- TODO: Update blank fragment layout -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="#+id/cctrct_search_submit"
android:layout_below="#+id/cctrct_p1"
android:layout_centerHorizontal="true"
android:layout_marginTop="65dp"
android:layout_gravity="center_horizontal|bottom" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Only the submit button works"
android:id="#+id/cctr_search_logo"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:layout_gravity="center_horizontal|top" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phase III"
android:id="#+id/cctrct_p3"
android:layout_toEndOf="#+id/cctrct_search_dsite"
android:layout_below="#+id/cctrct_search_pnum"
android:layout_toRightOf="#+id/cctrct_search_dsite"
android:layout_marginTop="28dp"
android:layout_gravity="center" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phase II"
android:id="#+id/cctrct_p2"
android:layout_toEndOf="#+id/cctrct_p1"
android:layout_alignTop="#+id/cctrct_p3"
android:layout_alignLeft="#+id/cctrct_search_keyword"
android:layout_alignStart="#+id/cctrct_search_keyword"
android:layout_alignRight="#+id/cctrct_search_dsite"
android:layout_alignEnd="#+id/cctrct_search_dsite"
android:layout_gravity="center" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phase I"
android:id="#+id/cctrct_p1"
android:layout_toStartOf="#+id/cctrct_search_pnum"
android:layout_alignTop="#+id/cctrct_p2"
android:layout_toLeftOf="#+id/cctrct_search_pnum"
android:layout_gravity="center" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Keyword"
android:id="#+id/cctrct_search_keyword"
android:layout_below="#+id/cctrct_search_dsite"
android:layout_alignRight="#+id/cctrct_search_submit"
android:layout_alignEnd="#+id/cctrct_search_submit"
android:layout_marginTop="26dp"
android:layout_gravity="left|center_vertical" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Disease Site"
android:id="#+id/cctrct_search_dsite"
android:layout_below="#+id/cctr_search_logo"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:layout_gravity="right|center_vertical" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Protocol Number"
android:id="#+id/cctrct_search_pnum"
android:layout_below="#+id/cctrct_search_keyword"
android:layout_centerHorizontal="true"
android:layout_marginTop="31dp"
android:layout_gravity="left|center_vertical" />
</RelativeLayout>
</FrameLayout>
I just figured I would add the rest of the code from my class:
#Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
switch (position) {
case 0: fragmentManager.beginTransaction()
.replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
.commit(); break;
case 1: fragmentManager.beginTransaction()
.replace(R.id.container, cctrct_search.newInstance("test1", "test2"))
.commit(); break;
case 2: fragmentManager.beginTransaction()
.replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
.commit(); break;
case 3: fragmentManager.beginTransaction()
.replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
.commit(); break;
}
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.cctr_ct_home);
break;
case 2:
mTitle = getString(R.string.cctr_ct_search);
break;
case 3:
mTitle = getString(R.string.cctr_ct_saved);
break;
case 4:
mTitle = getString(R.string.cctr_ct_recent);
break;
}
}
public void restoreActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.cctr_clinical_trials, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onFragmentInteraction(Uri uri) {
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_cctr_clinical_trials, container, false);
return rootView;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((cctr_clinical_trials) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}
}
It looks like cctrct_searchButton is null which means it is not being found in the view. Is a Button with the id cctr_search present in the activity_cctr_clinical_trials layout xml file?
For future users, Mr. Spidy was 100% correct, the onClickListener should have been in the onCreateView and not in the onCreate section:
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_cctr_clinical_trials, container, false);
Button cctrct_searchButton;
cctrct_searchButton = (Button)rootView.findViewById(R.id.cctr_search);
cctrct_searchButton.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View view) {
Fragment newFragment = new cctrct_search();
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
return rootView;
}
THANKS AGAIN MR. SPIDY

Categories

Resources