无法在操作栏中添加下拉导航

Unable to add drop-down navigation in action bar

我无法了解如何在 android studio 的操作栏中添加下拉导航。我已经看到 http://developer.android.com/guide/topics/ui/actionbar.html#Dropdown 和 google 用于添加下拉导航但不明白。我是 android-studio 和 android.

的新手

所以当我 运行 这个代码时:

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);
}

所以问题是:

When I run the application, it starts and closes itself within seconds without displaying anything.

当我评论最后两行时,程序可以运行,但如果我取消评论它们,应用程序将启动并自行关闭。最后两个语句也已弃用。

来自Logcat我创立:

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.access0(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)

还有更多错误...

您应该将您的代码放在这里以检查发生了什么,但这段代码适用于我的:

   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);

要使这项工作正常进行,您必须处理正在使用的操作栏库。在这里您可以检查您需要哪个库以及获取它的代码。它还包括一个导航下拉示例: developer.android.com/intl/es/guide/topics/ui/actionbar.html