I am unable to get how to add Drop-down navigation in the action bar in android studio. I have seen http://developer.android.com/guide/topics/ui/actionbar.html#Dropdown and google for adding drop down navigation but don't get it. I am new to android-studio and android.
So when I run this code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(this,R.array.activity_array, android.R.layout.simple_spinner_dropdown_item);
ActionBar bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
ActionBar.OnNavigationListener listener = new ActionBar.OnNavigationListener() {
@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
return false;
}
};
bar.setListNavigationCallbacks(mSpinnerAdapter, listener);
}
So problem is:
When I run the application, it starts and closes itself within seconds without displaying anything.
When I comment the last two lines then program works but if I uncomment them app starts and closes itself. Also the last two statements are deprecated.
From Logcat I founded:
02-06 12:24:20.809 18210-18210/com.mycompany.dropdown E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mycompany.dropdown/com.mycompany.dropdown.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2218)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2268)
at android.app.ActivityThread.access$600(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1263)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5162)
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:756)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:572)
at miui.dexspy.DexspyInstaller.main(DexspyInstaller.java:171)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.mycompany.dropdown.MainActivity.onCreate(MainActivity.java:37)
at android.app.Activity.performCreate(Activity.java:5135)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at com.lbe.security.service.core.client.b.x.callActivityOnCreate(Unknown Source)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2182)
and more errors...
You should put your code here to check what's happening, but this code worked for my:
SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(this,
R.array.activity_array, android.R.layout.simple_spinner_dropdown_item);
ActionBar bar = getActionBar(); //or getSupportActionBar() depending on your target version
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
ActionBar.OnNavigationListener listener = new ActionBar.OnNavigationListener() {
@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
return false;
}
};
bar.setListNavigationCallbacks(mSpinnerAdapter, listener);
TO make this working you have to take care of the action bar library that is being used. Here you can check which library you need and the code to get it. It also includes an example of navigation drop-down : developer.android.com/intl/es/guide/topics/ui/actionbar.html