Android newbie here.
I'm trying to set an onClick event on a LinearLayout. But I keep getting the error
Attempt to invoke virtual method 'void android.widget.LinearLayout.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
Which, if I understand correctly means that the reference is null. Which doesn't make sense to me since I'm calling it after onCreate
Here's my code, anybody have any clues what I'm doing wrong?
MainActivity - The place with HERE! is where the crash is occuring
package ...
import ...
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();
final NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
setNavHeaderOnClickAction();
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// ...
}
public void setNavHeaderOnClickAction(){
// HERE! Here's the problemsome area
LinearLayout navHeaderUser = (LinearLayout) findViewById(R.id.nav_header_user);
navHeaderUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Intent myIntent = new Intent(MainActivity.this, UserProfile.class);
// MainActivity.this.startActivity(myIntent);
// DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
// drawer.closeDrawer(GravityCompat.START);
}
});
}
}
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>
app_bar_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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.trevorwood.biggles.MainActivity">
<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 android:id="#+id/main_content" layout="#layout/content_blank"/>
<android.support.design.widget.AppBarLayout
android:id="#+id/search_button"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_gravity="top|end"
android:layout_margin="10dp"
android:background="#android:drawable/ic_menu_search">
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
nav_header_main.xml (layout) included in activity_main.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"
android:layout_width="match_parent"
android:layout_height="120dp"
android:background="#drawable/side_nav_bar"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<LinearLayout
android:id="#+id/nav_header_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:clickable="true">
<ImageView
android:id="#+id/imageView"
android:layout_width="60dp"
android:layout_height="60dp"
android:paddingTop="#dimen/nav_header_vertical_spacing"
app:srcCompat="#android:drawable/sym_def_app_icon"
android:clickable="false"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:clickable="false">
<TextView
android:id="#+id/textView"
android:layout_width="303dp"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:text="JohnSmith007"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:clickable="false"/>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1,500 points"
android:clickable="false"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
activity_main_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:id="#+id/grp1"
android:checkableBehavior="single">
<item
android:id="#+id/nav_home"
android:icon="#drawable/ic_menu_home"
android:title="Home" />
<item
android:id="#+id/nav_library"
android:icon="#drawable/ic_menu_book"
android:title="Library" />
<item
android:id="#+id/nav_create"
android:icon="#drawable/ic_menu_add"
android:title="Create" />
<item
android:id="#+id/nav_user"
android:icon="#drawable/ic_menu_user"
android:title="My Account" />
<item
android:id="#+id/nav_settings"
android:icon="#drawable/ic_menu_cog_wheel"
android:title="Settings" />
</group>
<group
android:id="#+id/grp2"
android:checkableBehavior="single">
<item
android:id="#+id/nav_about"
android:icon="#drawable/ic_menu_about"
android:title="About" />
<item
android:id="#+id/nav_sign_out"
android:icon="#drawable/ic_menu_sign_out"
android:title="Sign Out" />
</group>
</menu>
content_blank.xml
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"
android:text="content_blank"/>
</LinearLayout>
NavigationView header is usually included this way:
<android.support.design.widget.NavigationView
android:id="#+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/white"
app:headerLayout="#layout/main_drawer_header"
app:itemTextColor="#color/black"
app:menu="#menu/menu_main_drawer"
app:itemIconTint="#null"/>
Since support libraries version 23.1.0 NavigationView is using a RecyclerView and the header is added as one of RecyclerView's items.
Activity's findViewById won't find the header and its internals.
To get access to it you need to get the header from NavigationView and call findViewById relative to header view:
NavigationView navigationView = (NavigationView) findViewById(R.id.nvView);
View navHeaderview = navigationView.getHeaderView(0);
LinearLayout navHeaderUser = (LinearLayout) navHeaderview.findViewById(R.id.nav_header_user);
navHeaderUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Intent myIntent = new Intent(MainActivity.this, UserProfile.class);
// MainActivity.this.startActivity(myIntent);
// DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
// drawer.closeDrawer(GravityCompat.START);
}
});
Hope I got it right and it'll work for you!
Related
I have a menu created that I am trying to display in my Fragment. Everything works well if the fragment is included in the activities xml layout file. But if I try to add the fragment to the frameLayout via fragment transaction without changing any other implementation the menu will not appear.
Activity
Fragment frag = Fragment.newInstance();
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame, frag)
.commit();
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Fragment
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater
inflater) {
inflater.inflate(R.menu.menu, menu);
}
Activity Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<FrameLayout
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_weight=".60"
android:layout_height="0dp">
<!--<fragment-->
<!--android:id="#+id/frag"-->
<!--android:name="name"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent">-->
<!--</fragment>-->
</FrameLayout>
<FrameLayout
android:id="#+id/controls_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<fragment
android:id="#+id/controls"
android:name="ControlsFrag"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
</LinearLayout>
Fragment Layout
<?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"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/softGray"
android:clickable="true"
android:focusable="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:theme="#style/AppTheme.AppBarOverlay"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:collapsedTitleTextAppearance="#style/collapsedText"
app:contentScrim="#color/softGray"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:expandedTitleTextAppearance="#style/expandedText"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:title="#string/action_groups">
<android.support.v7.widget.AppCompatImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.5"
app:srcCompat="#drawable/ic_skyscraper" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="#+id/scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<android.support.v7.widget.RecyclerView
android:id="#+id/main_groups_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
Menu Layout
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<item
android:id="#+id/search"
android:enabled="true"
android:icon="#drawable/ic_search_black_24dp"
android:title="#string/string"
app:showAsAction="ifRoom"
/>
<item
android:id="#+id/edit"
android:enabled="true"
android:icon="#drawable/ic_edit_black_alpha_24dp"
android:title="#string/string"
app:showAsAction="ifRoom"
/>
<item
android:id="#+id/add"
android:enabled="true"
android:icon="#drawable/ic_add_black_24dp"
android:title="#string/string"
app:showAsAction="ifRoom"
/>
</menu>
I am currently creating a navigation menu without using fragments. I've seemingly created the working system that opens classes and keeps the navigation bar present at each stage by using a BaseActivity and inheriting it into the classes that need the drawer. However upon loading the app seemingly the 3 bar 'Hamburger' icon isn't present and neither is the toolbar at the top (See photo). The navigation bar does seemingly work as I can swipe from the left and click to load other classes. I feel like my onCreate() in my BaseActivity is missing something but with googling I can't seem to find a solution.
Image of current issue:
Image of Nav Bar Working with issue in background:
This is my base activity:
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
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.MenuItem;
public class BaseActivity extends AppCompatActivity {
DrawerLayout drawerLayout;
ActionBarDrawerToggle actionBarDrawerToggle;
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_closed);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem item) {
Intent anIntent;
switch (item.getItemId()) {
case R.id.nav_homepage:
anIntent = new Intent(getApplicationContext(), homepage.class);
startActivity(anIntent);
drawerLayout.closeDrawers();
break;
case R.id.nav_add_roster:
anIntent = new Intent(getApplicationContext(), add_roster.class);
startActivity(anIntent);
drawerLayout.closeDrawers();
break;
case R.id.nav_check_schedule:
anIntent = new Intent(getApplicationContext(), schedule.class);
startActivity(anIntent);
drawerLayout.closeDrawers();
break;
}
return false;
}
});
}
}
Here's my homepage which is what is loaded initially:
import android.os.Bundle;
import android.widget.FrameLayout;
public class homepage extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout contentFrameLayout = (FrameLayout) findViewById(R.id.content_frame); //Remember this is the FrameLayout area within your content_main.xml
getLayoutInflater().inflate(R.layout.homepage, contentFrameLayout);
}
}
Here's my activity main:
<?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
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/content_frame"/>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_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>
You can try this, add the code below in your BaseActivity :
#Override
public void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
actionBarDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
actionBarDrawerToggle.onConfigurationChanged(newConfig);
}
EDIT:
Enable the home button in your onCreate function of BaseActivity as well :
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
There is missing a toolbar layout in your xml :
<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">
<android.support.design.widget.CoordinatorLayout
android:id="#+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eeeeee">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/content_frame"/>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_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>
Hope this helps;
Sorry for my english.
Try this way,
<?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_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/navigation_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>
and in app_bar_toolbar.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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<RelativeLayout
android:id="#+id/relative_tool_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>
</RelativeLayout>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
and in content_main.xml, use this.
<?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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.esec_0052.fingertips.FingerTipMainActivity"
tools:showIn="#layout/app_bar_finger_tip_main">
<FrameLayout
android:id="#+id/frame_parent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
I'm trying to add menu item "done" in the action bar. I just copied and modified the code from another activity that has menu items showing. So it should work properly.
Here's the activity layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/md_blue_600"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways"/>
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:id="#+id/activity_credits"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:mContext="com.getsaveit.saveit.activities.CreditsActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18sp"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:text="#string/affiliator_category"/>
<com.toptoche.searchablespinnerlibrary.SearchableSpinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintText="Select category"
android:id="#+id/affiliator_category_spinner"/>
<ExpandableListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/rectangular_shap"
android:id="#+id/affiliators_listview"/>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
And choose_affiliator_menu.xml file :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:title="#string/done"
android:orderInCategory="1"
android:id="#+id/next_btn"
app:showAsAction="always" />
</menu>
And the java code itself:
public class CreditsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_credits);
Toolbar actionBar = (Toolbar) findViewById(R.id.toolbar);
actionBar.setTitle("Memberships");
//....
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.choose_affiliator_menu, menu);
return true;
}
}
You should use below method after initialization of Toolbar.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Case_history_review.java
This is one of my java to use the nav bar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("E-care");
setSupportActionBar(toolbar);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView view = (NavigationView) findViewById(R.id.navigation_view);
view.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
Toast.makeText(Case_history_review.this, menuItem.getItemId() + " pressed", Toast.LENGTH_LONG).show();
Log.d(R.id.nav_1+"", menuItem.getItemId() + " ");
Intent intent = new Intent();
switch (menuItem.getItemId())
{
case R.id.nav_1:
break;
case R.id.nav_2:
intent.setClass(Case_history_review.this,queueshow.class);
//intent .putExtra("name", "Hello B Activity");
startActivity(intent);
break;
case R.id.nav_3:
intent.setClass(Case_history_review.this,Appointmentcreate.class);
//intent .putExtra("name", "Hello B Activity");
startActivity(intent);
break;
case R.id.nav_4:
intent.setClass(Case_history_review.this, AlarmActivity.class);
startActivity(intent);
break;
case R.id.nav_5:
intent.setClass(Case_history_review.this, PatientReport.class);
startActivity(intent);
break;
case R.id.nav_6:
intent.setClass(Case_history_review.this, TimeList.class);
startActivity(intent);
//logout
break;
}
menuItem.setChecked(true);
drawerLayout.closeDrawers();
return true;
}
});
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle( this, drawerLayout, toolbar, R.string.drawer_open , R.string.drawer_close){
#Override
public void onDrawerClosed(View drawerView) {
super .onDrawerClosed(drawerView);
}
#Override
public void onDrawerOpened(View drawerView) {
super .onDrawerOpened(drawerView);
}
};
db = new SQLiteHandler(getApplicationContext());
HashMap<String, String> dbuser = db.getUserDetails();
TextView name = (TextView) view.findViewById(R.id.drawer_name);
String username = dbuser.get("name");
Log.d("naem",username);
name.setText(username);
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
CirculaireNetworkImageView photo = (CirculaireNetworkImageView) view.findViewById(R.id.drawer_thumbnail);
photo.setImageUrl("http://192.168.43.216/test/" + dbuser.get("image"), imageLoader);
drawerLayout.setDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
These two lines get errors
name.setText(username);
photo.setImageUrl("http://192.168.43.216/test/" + dbuser.get("image"), imageLoader);
Error message:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.yuen.e_carei.Case_history_review.onCreate(Case_history_review.java:116)
layout/drawer_header.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="150dp"
android:background="#0097a7"
>
<com.example.yuen.CirculaireNetworkImageView
android:id="#+id/drawer_thumbnail"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginRight="8dp"
android:elevation="8dp"
android:layout_above="#+id/drawer_name"
android:layout_alignStart="#+id/drawer_name" />
<TextView
android:id="#+id/drawer_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginStart="24dp"
android:layout_alignParentBottom="true"
android:text="Eddard Stark"
android:textSize="14sp"
android:textColor="#fff"
android:textStyle="bold"
android:paddingBottom="8dp"
/>
</RelativeLayout>
menu/drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_1"
android:checked="true"
android:icon="#drawable/hi"
android:title="Case Review"/>
<item
android:id="#+id/nav_2"
android:icon="#drawable/hi"
android:title="Queue state"/>
<item
android:id="#+id/nav_3"
android:icon="#drawable/hi"
android:title="Create appointemnt"/>
<item
android:id="#+id/nav_4"
android:icon="#drawable/hi"
android:title="Alarm"/>
<item
android:id="#+id/nav_5"
android:icon="#drawable/hi"
android:title="Report"/>
<item
android:id="#+id/nav_6"
android:icon="#drawable/hi"
android:title="Logout"/>
</group>
</menu>
activity_case_history_solo.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"
android:fitsSystemWindows="true"
tools:context="com.example.yuen.e_carei.Case_history_review">
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!-- your content layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<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>
<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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.yuen.e_carei.Case_history_review">
<ImageView
android:src="#drawable/hi"
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/imageView2"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/imageView2"
android:layout_alignBottom="#+id/imageView2"
android:id="#+id/relativeLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name : "
android:textSize="25sp"
android:id="#+id/textView2"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ID : "
android:textSize="25sp"
android:id="#+id/idTag"
android:paddingLeft="11dp"
android:layout_below="#+id/textView2"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chan Tai Man"
android:textSize="25sp"
android:id="#+id/nameResult"
android:paddingBottom="10dp"
android:layout_above="#+id/idResult"
android:layout_toEndOf="#+id/textView2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="N001"
android:textSize="25sp"
android:id="#+id/idResult"
android:layout_below="#+id/textView2"
android:layout_toEndOf="#+id/idTag" />
</RelativeLayout>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView_case_history"
android:layout_alignEnd="#+id/relativeLayout"
android:layout_below="#+id/imageView2" />
</RelativeLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/drawer_header"
app:menu="#menu/drawer" />
</android.support.v4.widget.DrawerLayout>
<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"
android:src="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
I want to change the text in the drawer_header to become other string. However, it can get the id but crash in the runtime. Please give me some helps to solve this. Thank You.
Error depicts that the TextView object is null, means there is no such textview exists in the layout that is currently running.
Did the textview present in the XML layout that you are calling? Which layout did you put the TextView?
Kindly check whether your activity is correct!!
The id that you use inside findViewById(R.id....) may be wrong. Double check that.
You have called the
TextView name = (TextView) view.findViewById(R.id.drawer_name);
in the Case_history_review.java but it seems that the above textView doesn't exists in your layout that you are calling. Probably you will be calling the below activity_case_history_solo.XML in the Activity and it doesn't has it. And the above TextView lies in the drawer_header.xml
Hope this helps you..!!
I have an activity that correctly displays my ActionBar and it's items, however when I try to add a fragment to that activity the ActionBar disappears when that fragment is launched, or if I try to include the ActionBar it shows the bar but no title or MenuOptions.
Want I am trying to achieve is one activity, with the same ActionBar for all fragments, unless specified otherwise.
MainActivity.Java
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final HomeFragment homeFragment = new HomeFragment();
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);
if (savedInstanceState == null) {
getFragmentManager()
.beginTransaction()
.add(R.id.main_container, homeFragment)
.addToBackStack(null)
.commit();
}
}
#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_activity_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;
} else if (id == R.id.search_mag_icon){
// Open intent here
Intent i = new Intent(getApplicationContext(), SearchActivity.class);
startActivity(i);
}
return super.onOptionsItemSelected(item);
}
HomeFragment.java
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- Book icon and text -->
<ImageView
android:id="#+id/bookIcon"
android:layout_width="100dip"
android:layout_height="100dip"
android:layout_marginLeft="50dip"
android:layout_marginTop="110dip"
android:src="#drawable/book" />
<TextView
android:id="#+id/bookLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/bookIcon"
android:layout_marginLeft="20dip"
android:text="#string/bookLabel"
android:textColor="#color/colorAlt"
android:textStyle="bold" />
<!-- Barcode icon and text -->
<ImageView
android:id="#+id/barcodeIcon"
android:layout_width="95dip"
android:layout_height="95dip"
android:layout_marginLeft="260dip"
android:layout_marginTop="120dip"
android:src="#drawable/barcode" />
<TextView
android:id="#+id/barcodeLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/barcodeIcon"
android:layout_marginLeft="260dip"
android:paddingTop="5dip"
android:text="#string/barcodeLabel"
android:textColor="#color/colorAlt"
android:textStyle="bold" />
<!-- Message icon and text -->
<ImageView
android:id="#+id/messageIcon"
android:layout_width="100dip"
android:layout_height="100dip"
android:layout_marginLeft="50dip"
android:layout_marginTop="350dip"
android:src="#drawable/email" />
<TextView
android:id="#+id/messageLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/messageIcon"
android:layout_marginLeft="55dip"
android:text="#string/messageLabel"
android:textColor="#color/colorAlt"
android:textStyle="bold" />
<!-- List icon and text -->
<ImageView
android:id="#+id/listIcon"
android:layout_width="95dip"
android:layout_height="95dip"
android:layout_marginLeft="260dip"
android:layout_marginTop="350dip"
android:src="#drawable/list" />
<TextView
android:id="#+id/listLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/listIcon"
android:layout_marginLeft="273dip"
android:paddingTop="5dip"
android:text="#string/listLabel"
android:textColor="#color/colorAlt"
android:textStyle="bold" />
</RelativeLayout>
</FrameLayout>
activity_main
<?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:background="#FFF"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="#layout/app_bar_main" />
<FrameLayout
android:id="#+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
<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"
android:background="#color/colorPrimaryDark"
app:itemTextColor="#FFF"
app:itemIconTint="#FFF"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<include layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Book icon and text -->
<ImageView
android:id="#+id/bookIcon"
android:src="#drawable/book"
android:layout_width="100dip"
android:layout_height="100dip"
android:layout_marginLeft="50dip"
android:layout_marginTop = "110dip"
/>
<TextView
android:id="#+id/bookLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/bookLabel"
android:textColor="#color/colorAlt"
android:textStyle="bold"
android:layout_marginLeft="20dip"
android:layout_below="#+id/bookIcon" />
<!-- Barcode icon and text -->
<ImageView
android:id="#+id/barcodeIcon"
android:src="#drawable/barcode"
android:layout_width="95dip"
android:layout_height="95dip"
android:layout_marginLeft="260dip"
android:layout_marginTop = "120dip"
/>
<TextView
android:id="#+id/barcodeLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/barcodeLabel"
android:textColor="#color/colorAlt"
android:textStyle="bold"
android:layout_below="#+id/barcodeIcon"
android:paddingTop="5dip"
android:layout_marginLeft="260dip"/>
<!-- Message icon and text -->
<ImageView
android:id="#+id/messageIcon"
android:src="#drawable/email"
android:layout_width="100dip"
android:layout_height="100dip"
android:layout_marginLeft="50dip"
android:layout_marginTop = "350dip"
/>
<TextView
android:id="#+id/messageLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/messageLabel"
android:textColor="#color/colorAlt"
android:textStyle="bold"
android:layout_marginLeft="55dip"
android:layout_below="#+id/messageIcon" />
<!-- List icon and text -->
<ImageView
android:id="#+id/listIcon"
android:src="#drawable/list"
android:layout_width="95dip"
android:layout_height="95dip"
android:layout_marginLeft="260dip"
android:layout_marginTop = "350dip"
/>
<TextView
android:id="#+id/listLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/listLabel"
android:textColor="#color/colorAlt"
android:textStyle="bold"
android:layout_below="#+id/listIcon"
android:paddingTop="5dip"
android:layout_marginLeft="273dip"/>
</RelativeLayout>
Thank you for any help in advance.
modify layout
Put you ToolBar inside LinearLayout and Change Fragment with FrameLayout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="#layout/app_bar_main" />
<FrameLayout
android:id="#+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
call Fragment from Activity
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.main_container,homeFragment));
fragmentTransaction.commit();