rockers rockers - 3 months ago 14
Android Question

NullPointerException error while loading Navigation Drawer

I am using code from the developer website (http://developer.android.com/training/implementing-navigation/nav-drawer.html) to install a navigation drawer, however, I am getting a NullPointerException in this method:

09-03 06:08:01.431: E/AndroidRuntime(1081): FATAL EXCEPTION: main
09-03 06:08:01.431: E/AndroidRuntime(1081): Process: com.example.navigationsamp, PID: 1081
09-03 06:08:01.431: E/AndroidRuntime(1081): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.navigationsamp/com.example.navigationsamp.MainActivity}: java.lang.NullPointerException
09-03 06:08:01.431: E/AndroidRuntime(1081): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
09-03 06:08:01.431: E/AndroidRuntime(1081): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
09-03 06:08:01.431: E/AndroidRuntime(1081): at android.app.ActivityThread.access$800(ActivityThread.java:135)
09-03 06:08:01.431: E/AndroidRuntime(1081): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-03 06:08:01.431: E/AndroidRuntime(1081): at android.os.Handler.dispatchMessage(Handler.java:102)
09-03 06:08:01.431: E/AndroidRuntime(1081): at android.os.Looper.loop(Looper.java:136)
09-03 06:08:01.431: E/AndroidRuntime(1081): at android.app.ActivityThread.main(ActivityThread.java:5001)
09-03 06:08:01.431: E/AndroidRuntime(1081): at java.lang.reflect.Method.invokeNative(Native Method)
09-03 06:08:01.431: E/AndroidRuntime(1081): at java.lang.reflect.Method.invoke(Method.java:515)
09-03 06:08:01.431: E/AndroidRuntime(1081): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
09-03 06:08:01.431: E/AndroidRuntime(1081): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
09-03 06:08:01.431: E/AndroidRuntime(1081): at dalvik.system.NativeStart.main(Native Method)
09-03 06:08:01.431: E/AndroidRuntime(1081): Caused by: java.lang.NullPointerException
09-03 06:08:01.431: E/AndroidRuntime(1081): at com.example.navigationsamp.MainActivity.onCreate(MainActivity.java:58)
09-03 06:08:01.431: E/AndroidRuntime(1081): at android.app.Activity.performCreate(Activity.java:5231)
09-03 06:08:01.431: E/AndroidRuntime(1081): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-03 06:08:01.431: E/AndroidRuntime(1081): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
09-03 06:08:01.431: E/AndroidRuntime(1081): ... 11 more


The rest of code:

MainActivity.java

package com.example.navigationsamp;

import java.util.Locale;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.SearchManager;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;


public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;

private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mPlanetTitles;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mTitle = mDrawerTitle = getTitle();
mPlanetTitles = getResources().getStringArray(R.array.planets_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);


mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mPlanetTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());


getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);


mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}

public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);

if (savedInstanceState == null) {
selectItem(0);
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}


@Override
public boolean onPrepareOptionsMenu(Menu menu) {

boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}

switch(item.getItemId()) {
case R.id.action_websearch:

Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, getActionBar().getTitle());

if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} else {
Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}


private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}

private void selectItem(int position) {

Fragment fragment = new PlanetFragment();
Bundle args = new Bundle();
args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
fragment.setArguments(args);

FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();


mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}

@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}



@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);

mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);

mDrawerToggle.onConfigurationChanged(newConfig);
}

public static class PlanetFragment extends Fragment {
public static final String ARG_PLANET_NUMBER = "planet_number";

public PlanetFragment() {

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_planet, container, false);
int i = getArguments().getInt(ARG_PLANET_NUMBER);
String planet = getResources().getStringArray(R.array.planets_array)[i];

int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()),
"drawable", getActivity().getPackageName());
((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId);
getActivity().setTitle(planet);
return rootView;
}
}
}


activity_main.xml

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >

</FrameLayout>
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>


drawer_list_item.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#fff"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"/>


fragment_planet.xml

**<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:gravity="center"
android:padding="32dp" />**


main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/action_websearch"
android:icon="@drawable/action_search"
android:title="@string/action_websearch"
android:showAsAction="ifRoom|withText" />

</menu>

Answer

In your manifest file add:

 android:theme="@android:style/Theme.Holo.Light.DarkActionBar"

This supports getActionBar() so there will be no NullPointerException and thus no java.lang.RuntimeException

Comments