This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 2 years ago.
Working on an android project just for the fun of it. I am getting back the error after a user tries to log in. I did a try-catch around the line where the error is and the RecyclerView object itself is null. So now that I know that, I am trying to figure out why it isn't getting set when I do findViewById(). I will paste the relevant code down below.
java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)' on a null object reference
MainActivity.java
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AbsListView;
import android.widget.Adapter;
import android.widget.SearchView;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
MyProductAdapter myProductAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
BottomNavigationView navView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications).build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
RecyclerView recyclerView;
recyclerView = findViewById(R.id.listId);
System.out.println("This is recyclverView" + recyclerView);
try {
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
} catch (NullPointerException e) {
System.out.println("null here" + e.getCause());
}
ArrayList<MyProduct> myProductData = new ArrayList<>();
myProductData.add(new MyProduct("Panadol", "Pill for headAche", "Price : $2.99", R.drawable.logo2));
myProductData.add(new MyProduct("Nyquill", "Pill for headAche", "Price : $2.99", R.drawable.logo2));
myProductData.add(new MyProduct("ZAck", "Pill for headAche", "Price : $2.99", R.drawable.logo2));
myProductData.add(new MyProduct("Codeine", "Pill for headAche", "Price : $2.99", R.drawable.logo2));
myProductAdapter = new MyProductAdapter(myProductData, MainActivity.this);
recyclerView.setAdapter(myProductAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_bar, menu);
MenuItem menuItem = menu.findItem(R.id.search);
SearchView searchView = (SearchView) menuItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s) {
return false;
}
#Override
public boolean onQueryTextChange(String s) {
myProductAdapter.getFilter().filter(s);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
}
fragment_dashboard.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.dashboard.DashboardFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/listId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
/>
</LinearLayout>
DashBoardFragment.java
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import com.example.medistak.R;
public class DashboardFragment extends Fragment {
private DashboardViewModel dashboardViewModel;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
dashboardViewModel =
new ViewModelProvider(this).get(DashboardViewModel.class);
View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
dashboardViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
#Override
public void onChanged(#Nullable String s) {
}
});
return root;
}
}
Edit (after OP adds the DashboardFragment class)
Move your MyProductAdapter and recyclerView code from MainActivity to DashboardFragment onCreateView() method as follows:
RecyclerView recyclerView = findViewById(R.id.listId);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(myProductAdapter);
Your RecyclerView listId resides in your DashboardFragment whereas you are initializing and setting it up in your MainActivity class. The reason why Null Object Reference error is showing up is because of the order in which Activities and their underlying Fragments are added to the Backstack.
First your MainActivity is setup. Then follows the Fragments lying in MainActivity, in your case its the DashboardFragment. So if you will setup a recyclerView of the DashboardFragment which is not even in existence from MainActivity's point of view and yet to be created , of course it will lead to a NPE.
The solution to your problem will be to pluck out all the recyclerView setup code from MainActivity class and transfer it over to DashboardFragment.
Related
Below are my both activities which i am calling .
I know this is answered before but my case seems to be different.
Can somebody look into this?
import android.os.Bundle;
import android.view.View;
import com.example.mediapipeposetracking.databinding.ActivityDashboardBinding;
public class DashboardActivity extends DrawerBaseActivity{
ActivityDashboardBinding activityDashboardBinding;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityDashboardBinding = ActivityDashboardBinding.inflate(getLayoutInflater());
setContentView(activityDashboardBinding.getRoot());
}
}
Second Activity
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import com.google.android.material.navigation.NavigationView;
public class DrawerBaseActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawerLayout;
#Override
public void setContentView(View view) {
drawerLayout=(DrawerLayout) getLayoutInflater().inflate(R.layout.activity_drawer_base,null);
FrameLayout container=drawerLayout.findViewById(R.id.activityContainer);
container.addView(view);
super.setContentView(view);
Toolbar toolbar =drawerLayout.findViewById(R.id.toolBar);
setSupportActionBar(toolbar);
NavigationView navigationView=drawerLayout.findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawerLayout,R.string.menu_drawer_open, R.string.menu_drawer_close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
return false;
}
protected void allocateActivityTitle(String titleString){
if(getSupportActionBar()!=null){
getSupportActionBar().setTitle(titleString);
}
}
}
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:5235)
at android.view.ViewGroup.addView(ViewGroup.java:5064)
at android.view.ViewGroup.addView(ViewGroup.java:5004)
at android.view.ViewGroup.addView(ViewGroup.java:4976)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:687)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:175)
at com.example.mediapipeposetracking.DrawerBaseActivity.setContentView(DrawerBaseActivity.java:27)
at com.example.mediapipeposetracking.DashboardActivity.onCreate(DashboardActivity.java:17)
I have a bottom navigation bar with 4 buttons. One of them is a back-button that just navigates back to the previously displayed fragment (I use a one-activity multiple-fragments approach). Basically everything works fine. Now I wanted to know whether it is possible not to navigate back to a certain fragment A? So basically whenever this Fragement A was the last fragment that was being displayed and the user clicks on the back-button, it should not navigate back but rather stay on the current fragment (and maybe display a small toast message).
So here you can see my main activity where the navigation is implemented:
package com.example.td.bapp;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentManager;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import com.example.td.bapp.databinding.ActivityMainBinding;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
public class MainActivity extends AppCompatActivity {
public static DataBaseHelper myDB;
public static boolean veryFirstCreation = true;
private ActivityMainBinding binding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
setContentView(view);
final NavController navController = Navigation.findNavController(this,
R.id.navHostfragment);
NavigationUI.setupWithNavController(binding.bottomNavigation, navController);
myDB=new DataBaseHelper(this);
if(veryFirstCreation) {
navController.navigate(R.id.FR_LanguageSelection);
veryFirstCreation = false;
}
// Define the bahaviour when clicking on items of the bottomNavigationBar; Overwrites the JetpackNavigation behaviour (automatic navigation using the id specified in the BottomNavigation XML menu file)
binding.bottomNavigation.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
if (item.getItemId()== R.id.BottomNavigation_Back) {
//Here I navigate back to the previous fragment
navController.navigateUp();
}
if (item.getItemId()== R.id.BottomNavigation_Language) {
navController.navigate(R.id.FR_LanguageSelection);
}
if (item.getItemId()== R.id.BottomNavigation_MyItems) {
navController.navigate(R.id.FR_MyItems);
}
if (item.getItemId()== R.id.BottomNavigation_Menu) {
navController.navigate(R.id.FR_Menu);
}
return true;
}
});
};
}
I'd appreciate every comment and would be quite thankful for your help.
You could use the SupportFragmentManager, with this you could know if your back stack contains at least one fragment. Use this in your back function (or in your onBackPressed if necessary)
FragmentManager fm = getSupportFragmentManager();
if (fm.getBackStackEntryCount() ==1) {
Log.d("ppl","This is the last fragment. Show your message, poppup or whatever you want here")
} else if (fm.getBackStackEntryCount() >1) {
Log.d("ppl","the backstack still have more to show")
fm.popBackStackImmediate();
}
I deleted a UI package which contained my fragment classes.
The UI package I deleted
I then created new classes of the same name in the bustracker package
What my project set up looks like now
When I run the app I get the following error.
caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.bustracker.ui.nearbyStops.nearbyStopsFragment" on path: DexPathList[[zip file "/data/app/com.example.bustracker-cuWeKLTM_oUFXduWOdcaDA==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.bustracker-cuWeKLTM_oUFXduWOdcaDA==/lib/x86, /system/lib, /vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
... 55 more
My question is, how do I get it to no longer look for com.example.bustracker.ui.nearbyStops.nearbyStopsFragment, but instead look for com.example.bustracker.nearbyStopsFragment?
Project has 4 classes, all of which you can find below.
MainActivity.java
import android.os.Bundle;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.view.Menu;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.navigation.NavigationView;
import androidx.annotation.NonNull;
import androidx.core.view.GravityCompat;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
/*FloatingActionButton fab = 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 = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_nearby_stops, R.id.nav_saved, R.id.nav_search)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.nav_nearby_stops:
getSupportFragmentManager().beginTransaction().replace(R.id.nav_view, new nearbyStopsFragment()).commit();
break;
case R.id.nav_saved:
getSupportFragmentManager().beginTransaction().replace(R.id.nav_view, new savedFragment()).commit();
break;
case R.id.nav_search:
getSupportFragmentManager().beginTransaction().replace(R.id.nav_view, new searchFragment()).commit();
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
});
}
#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 onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}
nearbyStopsFragment.java
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class nearbyStopsFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_nearby_stops, container, false);
}
}
savedFragment.java
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class savedFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_saved, container, false);
}
}
searchFragment.java
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class searchFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_search, container, false);
}
}
It's hard to know without seeing the code, but it looks like something in BaseDexClassLoader is trying to call something in the fragment file on line 125.
Inside your activity/fragment you will have, on top of the class, a lot of imports of packages you are using. There should be an import that says something like this:
import com.example.bustracker.ui.nearbyStops.nearbyStopsFragment;
Just change that into this new one:
import com.example.bustracker.nearbyStopsFragment;
If this doesn't work please provide your JAVA code with imports.
I faced this problem when I converted some Java classes to Kotlin, So I find out that I need to add id "kotlin-android" plugin and implementation "androidx.core:core-ktx:1.7.0" in Module gradle and classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10" in project gradle
I'm trying to build a food menu app for my school. I will be entering the meal plan every week into a MySQL database. My app has a tabbed view with each fragment representing the day of the week.
I want to be able to display the corresponding day's menu on the corresponding fragment.
MainActivity.java
package me.anshsehgal.lunchmenu;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.app.FragmentManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
TabLayout tabLayout;
ViewPager viewPager;
ViewPagerAdapter viewPagerAdapter; //make all the variables
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar)findViewById(R.id.toolBar);
setSupportActionBar(toolbar);
tabLayout = (TabLayout)findViewById(R.id.tabLayout);
viewPager = (ViewPager)findViewById(R.id.viewPager); //reference them
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPagerAdapter.addFragments(new HomeFragment(),"M");
viewPagerAdapter.addFragments(new MondayFragment(),"Tue");
viewPagerAdapter.addFragments(new TuesdayFragment(),"W");
viewPagerAdapter.addFragments(new WednesdayFragment(),"Thu");
viewPagerAdapter.addFragments(new ThursdayFragment(),"F"); //adding fragments to view page adapter
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_layout,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
RelativeLayout main_view = (RelativeLayout)findViewById(R.id.anshLayout);
switch (item.getItemId()){
case R.id.action_settings:
if(item.isChecked())
item.setChecked(false);
else
item.setChecked(true);
Intent i = new Intent(this,Settings.class);
startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
MondayFragment.java
package me.anshsehgal.lunchmenu;
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 MondayFragment extends Fragment {
public MondayFragment() {
// 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_monday, container, false);
}
}
Screenshot of app
Sample of the layout
Sample entry in table
It is not advisable to access my sql directly in Android applications. You need to build rest API's which will expose required data in compressed manner(JSON). You can use any of the programming language like Java/C#/Scala/Node to expose rest end points.
If you are trying to store data which is user specific then you can use sql lite database
I tried to have a view of an ActionBarActivity opening after hitting a spinner button.
There are two items on my spinner, the second one runs fine, but when I tried to access the Categorias item the app throws me a NullPointerException inside the DrawerActivity class. I don't actually know where the problem is. Another ActionBarActivity extension class that I have runs perfectly.
I'm new to Android/Java development.
The Spinner Fragment
import inmostla.ligatangamanga.pruebaintegrar.navigationdrawer.NavigationDrawerActivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
public class SpinnerFragment extends Fragment {
private static Spinner spinner;
private int selected;
private View mView;
static void setSpinnerContent( View view ){
spinner = (Spinner) view.findViewById(R.id.spinner1);
return ;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_spinner, container, false);
// Now use the above view to populate the spinner.
setSpinnerContent( view );
/**
* Maneja las acciones seleccionadas del Spinner
*/
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
selected = spinner.getSelectedItemPosition();
switch(selected){
case 1:
Intent categorias = new Intent( );
categorias.setClass( getActivity() , NavigationDrawerActivity.class );
startActivity(categorias);
break;
case 2:
Intent convenios = new Intent();
convenios.setClass(getActivity(), ConveniosFragment.class);
startActivity(convenios);
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
return view;
}
}
The Navigation Drawer Activity extends a ActionBarActivity
package inmostla.ligatangamanga.pruebaintegrar.navigationdrawer;
import inmostla.ligatangamanga.pruebaintegrar.navigationdrawer.NavigationDrawerFragment;
import inmostla.ligatangamanga.pruebaintegrar.R;
import android.app.Activity;
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.support.v4.widget.DrawerLayout;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class NavigationDrawerActivity extends ActionBarActivity implements
NavigationDrawerFragment.NavigationDrawerCallbacks {
/**
* 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;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.navigation_drawer);
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));
}...
Maybe that's because getActivity() returns null. Try overriding onAttach() and keep Activity reference as a field in your class. Use this reference instead of getActivity() when you needed a reference to context or activity.