SlidingMenu教程:getActionBar()。setDisplayHomeAsUpEnabled(true);在新活动创建

时间:2015-06-11 00:18:12

标签: java android android-fragments android-actionbar slidingmenu

我正在尝试这个教程:http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/

在应用程序中创建一个滑动菜单。

幸运的是,

  

getActionBar()setDisplayHomeAsUpEnabled(真);

抛出空指针异常。

我是Android的初学者,我已经阅读了几个解决方案,但似乎没有人能够正常使用,当应用程序到达这部分代码时,应用程序仍会崩溃。

我读到了一些问题: getActionBar().setDisplayHomeAsUpEnabled(true) throws NullPointerExceptiongetActionBar().setDisplayHomeAsUpEnabled(true); throws NullPointerException on new activity creation (Google - Basic Tutorial)getActionBar().setDisplayHomeAsUpEnabled(true); nullpointer in preferenceActivity

有人可以帮我吗?显然它涉及动作栏,但我不知道到底是什么......

非常有帮助,谢谢!

以下是显示菜单的MainActivity的代码, 在它下面我提供了错误日志 :

  

package com.example.invite.myapplication.activities;

import com.example.invite.myapplication.adapter.NavDrawerListAdapter;
import com.example.invite.myapplication.model.NavDrawerItem;
import com.example.invite.myapplication.R;

import com.example.invite.myapplication.frags.*;


import java.util.ArrayList;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

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

    // nav drawer title
    private CharSequence mDrawerTitle;×

    // used to store app title
    private CharSequence mTitle;

    // slide menu items
    private String[] navMenuTitles;
    private TypedArray navMenuIcons;

    private ArrayList<NavDrawerItem> navDrawerItems;
    private NavDrawerListAdapter adapter;

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

        mTitle = mDrawerTitle = getTitle();

        // load slide menu items
        navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);

        // nav drawer icons from resources
        navMenuIcons = getResources()
                .obtainTypedArray(R.array.nav_drawer_icons);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.list_slidermenu);

        navDrawerItems = new ArrayList<NavDrawerItem>();

        // adding nav drawer items to array
        // Home
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
        // Find People
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
        // Photos
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
        // Communities, Will add a counter here
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1), true, "22"));
        // Pages
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
        // What's hot, We  will add a counter here
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));


        // Recycle the typed array
        navMenuIcons.recycle();

        mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

        // setting the nav drawer list adapter
        adapter = new NavDrawerListAdapter(getApplicationContext(),
                navDrawerItems);
        mDrawerList.setAdapter(adapter);

        // enabling action bar app icon and behaving it as toggle button
        **getActionBar().setDisplayHomeAsUpEnabled(true);**
        getActionBar().setHomeButtonEnabled(true);

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, //nav menu toggle icon
                R.string.app_name, // nav drawer open - description for accessibility
                R.string.app_name // nav drawer close - description for accessibility
        ) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(mTitle);
                // calling onPrepareOptionsMenu() to show action bar icons
                invalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(mDrawerTitle);
                // calling onPrepareOptionsMenu() to hide action bar icons
                invalidateOptionsMenu();
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            // on first time display view for first nav item
            displayView(0);
        }
    }

这里是错误日志:

  

06-11 02:12:24.364 25134-25134 / com.example.invite.myapplication   E / AndroidRuntime:致命异常:主要       java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.invite.myapplication / com.example.invite.myapplication.activities.MainActivity}:   显示java.lang.NullPointerException               在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)               在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)               在android.app.ActivityThread.access $ 700(ActivityThread.java:159)               在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1316)               在android.os.Handler.dispatchMessage(Handler.java:99)               在android.os.Looper.loop(Looper.java:137)               在android.app.ActivityThread.main(ActivityThread.java:5419)               at java.lang.reflect.Method.invokeNative(Native Method)               在java.lang.reflect.Method.invoke(Method.java:525)               在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1187)               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)               at dalvik.system.NativeStart.main(Native Method)        引起:java.lang.NullPointerException               at com.example.invite.myapplication.activities.MainActivity.onCreate(MainActivity.java:91)               在android.app.Activity.performCreate(Activity.java:5372)               在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)               在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)   在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)   在android.app.ActivityThread.access $ 700(ActivityThread.java:159)   在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1316)   在android.os.Handler.dispatchMessage(Handler.java:99)   在android.os.Looper.loop(Looper.java:137)   在android.app.ActivityThread.main(ActivityThread.java:5419)   at java.lang.reflect.Method.invokeNative(Native Method)   在java.lang.reflect.Method.invoke(Method.java:525)   在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1187)   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)   在dalvik.system.NativeStart.main(本地方法)

这是“styles.xml”

  

<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

<style name = "NoActionBar" parent = "@android:style/Theme.Holo.Light">
    <!-- <item name = "android:windowActionBar">false</item> -->
    <!-- <item name = "android:windowNoTitle">true</item> -->
</style>
     

     

解决方案:#(希望它也适合你)。

     

最后很简单。

     

只需添加:

     
    

requestWindowFeature(Window.FEATURE_ACTION_BAR);

  
     在SetContentView(View ..)之前

     

据我了解,这种强制活动要求   行动吧“行动吧请!(?)”。

     

并在清单

中设置min sdk 11和target 17      

这里的答案最终帮助了我:   https://stackoverflow.com/a/10031400

3 个答案:

答案 0 :(得分:0)

在OnCreate()中声明此代码alert(5 > x); 之后,您必须在getActionBar().setDisplayHomeAsUpEnabled(true);中声明一些代码,如下所示:

OnOptionItemSelected()

希望它有所帮助:D

答案 1 :(得分:0)

尝试像这样运行

private ActionBar actionBar;

 @Override
    protected void onCreate(Bundle savedInstanceState) {

  actionBar = getActionBar();
        if(actionBar!=null) {
           actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeButtonEnabled(true);
        }

}

答案 2 :(得分:0)

我刚刚找到了解决方案。

最后很简单。

只需添加:

  

requestWindowFeature(Window.FEATURE_ACTION_BAR);

在SetContentView(View ..)之前

并在清单

中设置min sdk 11和target 17

最后帮助我的答案是:https://stackoverflow.com/a/10031400