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

时间:2015-02-05 09:37:22

标签: android android-studio android-actionbar android-actionbar-compat

我无法获得如何在android studio的操作栏中添加下拉导航。我看过http://developer.android.com/guide/topics/ui/actionbar.html#Dropdown和谷歌添加下拉导航但是没有得到它。我是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);
}

问题是:

  

当我运行应用程序时,它会在几秒钟内启动并关闭,而不显示任何内容。

drop-down

当我评论最后两行时,程序可以正常工作,但如果我取消注释它们,app会启动并自行关闭。最后两个语句也已弃用。

从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.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)

以及更多错误......

1 个答案:

答案 0 :(得分:0)

您应该将代码放在此处以检查发生了什么,但此代码适用于我:

   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