具有多个活动的导航抽屉。 - 空指针异常

Posted

技术标签:

【中文标题】具有多个活动的导航抽屉。 - 空指针异常【英文标题】:Navigation Drawer with Multiple activities. - NullPointerException 【发布时间】:2015-03-04 01:48:48 【问题描述】:

我正在通过 android Studio 开发我的移动应用程序。我的应用程序的启动器活动是SplashScreen。带有IntentSplashScreen 导航到我的HomeActivity。我尝试了this博客中的代码,这样我就可以有一个导航抽屉,但在活动之间而不是片段之间。

SplashscreenHomeActivity 活动中,我不想拥有这个抽屉。在HomeActivity 中有一个按钮(signin)。当您按下它时,您通常会导航到 SearchActivity,这是我想要拥有导航抽屉的第一页。

在上面的示例中,启动器活动是BaseActivity。因为我不希望BaseActivity 成为我的启动器活动,正如我所描述的,我在单击HomeActivity 内的signin 按钮后导航到BaseActivity


我的 HomeActivity.java:

import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ViewAnimator;
import common.activities.SampleActivityBase;
import common.logger.Log;
import common.logger.LogFragment;
import common.logger.LogWrapper;
import common.logger.MessageOnlyLogFilter;


public class HomeActivity extends SampleActivityBase 

    public static final String TAG = "HomeActivity";

    // Whether the Log Fragment is currently shown
    private boolean mLogShown;

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

        final ImageButton send = (ImageButton)findViewById(R.id.signin);

        send.setOnClickListener(new View.OnClickListener()
        
            public void onClick(View view)
            
                Intent intent = new Intent(HomeActivity.this, BaseActivity.class);
                startActivity(intent);
                finish();
            
        );

        if (savedInstanceState == null) 
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            SlidingTabsColorsFragment fragment = new SlidingTabsColorsFragment();
            transaction.replace(R.id.sample_content_fragment, fragment);
            transaction.commit();
        
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) 
        MenuItem logToggle = menu.findItem(R.id.menu_toggle_log);
        logToggle.setVisible(findViewById(R.id.sample_output) instanceof ViewAnimator);
        logToggle.setTitle(mLogShown ? "hide" : "show");

        return super.onPrepareOptionsMenu(menu);
    

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
//        switch(item.getItemId()) 
//            case R.id.menu_toggle_log:
//                mLogShown = !mLogShown;
//                ViewAnimator output = (ViewAnimator) findViewById(R.id.sample_output);
//                if (mLogShown) 
//                    //output.setDisplayedChild(1);
//                 else 
//                    //output.setDisplayedChild(0);
//                
//                supportInvalidateOptionsMenu();
//                return true;
//        
        return super.onOptionsItemSelected(item);
    

    /** Create a chain of targets that will receive log data */
    @Override
    public void initializeLogging() 
        // Wraps Android's native log framework.
        LogWrapper logWrapper = new LogWrapper();
        // Using Log, front-end to the logging chain, emulates android.util.log method signatures.
        Log.setLogNode(logWrapper);

        // Filter strips out everything except the message text.
        MessageOnlyLogFilter msgFilter = new MessageOnlyLogFilter();
        logWrapper.setNext(msgFilter);

        // On screen logging via a fragment with a TextView.
//        LogFragment logFragment = (LogFragment) getSupportFragmentManager()
//                .findFragmentById(R.id.log_fragment);
//        msgFilter.setNext(logFragment.getLogView());
//
//        Log.i(TAG, "Ready");
    



我的基本活动:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.Toast;

/**
 * @author dipenp
 *
 * This activity will add Navigation Drawer for our application and all the code related to navigation drawer.
 * We are going to extend all our other activites from this BaseActivity so that every activity will have Navigation Drawer in it.
 * This activity layout contain one frame layout in which we will add our child activity layout.
 */
public class BaseActivity extends Activity 

    /**
     *  Frame layout: Which is going to be used as parent layout for child activity layout.
     *  This layout is protected so that child activity can access this
     *  */
    protected FrameLayout frameLayout;

    /**
     * ListView to add navigation drawer item in it.
     * We have made it protected to access it in child class. We will just use it in child class to make item selected according to activity opened.
     */

    protected ListView mDrawerList;

    /**
     * List item array for navigation drawer items.
     * */
    protected String[] listArray =  "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" ;

    /**
     * Static variable for selected item position. Which can be used in child activity to know which item is selected from the list.
     * */
    protected static int position;

    /**
     *  This flag is used just to check that launcher activity is called first time
     *  so that we can open appropriate Activity on launch and make list item position selected accordingly.
     * */
    private static boolean isLaunch = true;

    /**
     *  Base layout node of this Activity.
     * */
    private DrawerLayout mDrawerLayout;

    /**
     * Drawer listner class for drawer open, close etc.
     */
    private ActionBarDrawerToggle actionBarDrawerToggle;


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

        frameLayout = (FrameLayout)findViewById(R.id.content_frame);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

        // set a custom shadow that overlays the main content when the drawer opens
        //mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

        // set up the drawer's list view with items and click listener
        mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, listArray));
        mDrawerList.setOnItemClickListener(new OnItemClickListener() 

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) 

                openActivity(position);
            
        );

        // enable ActionBar app icon to behave as action to toggle nav drawer
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        // ActionBarDrawerToggle ties together the the proper interactions between the sliding drawer and the action bar app icon
        actionBarDrawerToggle = new ActionBarDrawerToggle(
                this,                       /* host Activity */
                mDrawerLayout,              /* DrawerLayout object */
                R.drawable.ic_launcher,     /* nav drawer image to replace 'Up' caret */
                R.string.drawer_open,       /* "open drawer" description for accessibility */
                R.string.drawer_close)      /* "close drawer" description for accessibility */
        
            @Override
            public void onDrawerClosed(View drawerView) 
                getActionBar().setTitle(listArray[position]);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                super.onDrawerClosed(drawerView);
            

            @Override
            public void onDrawerOpened(View drawerView) 
                getActionBar().setTitle(getString(R.string.app_name));
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                super.onDrawerOpened(drawerView);
            

            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) 
                super.onDrawerSlide(drawerView, slideOffset);
            

            @Override
            public void onDrawerStateChanged(int newState) 
                super.onDrawerStateChanged(newState);
            
        ;
        mDrawerLayout.setDrawerListener(actionBarDrawerToggle);


        /**
         * As we are calling BaseActivity from manifest file and this base activity is intended just to add navigation drawer in our app.
         * We have to open some activity with layout on launch. So we are checking if this BaseActivity is called first time then we are opening our first activity.
         * */
        if(isLaunch)
            /**
             *Setting this flag false so that next time it will not open our first activity.
             *We have to use this flag because we are using this BaseActivity as parent activity to our other activity.
             *In this case this base activity will always be call when any child activity will launch.
             */
            isLaunch = false;
            openActivity(0);
        
    

    /**
     * @param position
     *
     * Launching activity when any list item is clicked.
     */
    protected void openActivity(int position) 

        /**
         * We can set title & itemChecked here but as this BaseActivity is parent for other activity,
         * So whenever any activity is going to launch this BaseActivity is also going to be called and
         * it will reset this value because of initialization in onCreate method.
         * So that we are setting this in child activity.
         */
//      mDrawerList.setItemChecked(position, true);
//      setTitle(listArray[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
        BaseActivity.position = position; //Setting currently selected position in this field so that it will be available in our child activities.

        switch (position) 
            case 0:
                startActivity(new Intent(this, SearchActivity.class));
                break;
            case 1:
                startActivity(new Intent(this, SearchActivity.class));
                break;
            case 2:
                startActivity(new Intent(this, SearchActivity.class));
                break;
            case 3:
                startActivity(new Intent(this, SearchActivity.class));
                break;
            case 4:
                startActivity(new Intent(this, SearchActivity.class));
                break;

            default:
                break;
        

        Toast.makeText(this, "Selected Item Position::"+position, Toast.LENGTH_LONG).show();
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 

        getMenuInflater().inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 

        // The action bar home/up action should open or close the drawer.
        // ActionBarDrawerToggle will take care of this.
        if (actionBarDrawerToggle.onOptionsItemSelected(item)) 
            return true;
        

        switch (item.getItemId()) 
            case R.id.action_settings:
                return true;

            default:
                return super.onOptionsItemSelected(item);
        
    

    /* Called whenever we call invalidateOptionsMenu() */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) 
        // If the nav drawer is open, hide action items related to the content view
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    

    /* We can override onBackPressed method to toggle navigation drawer*/
    @Override
    public void onBackPressed() 
        if(mDrawerLayout.isDrawerOpen(mDrawerList))
            mDrawerLayout.closeDrawer(mDrawerList);
        else 
            mDrawerLayout.openDrawer(mDrawerList);
        
    


我的搜索活动:

import android.content.Context;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.os.Bundle;
import android.widget.ImageView;


public class SearchActivity extends BaseActivity 

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);

        /**
         *  We will not use setContentView in this activty
         *  Rather than we will use layout inflater to add view in FrameLayout of our base activity layout*/

        /**
         * Adding our layout to parent class frame layout.
         */
        getLayoutInflater().inflate(R.layout.activity_search, frameLayout);

        /**
         * Setting title and itemChecked
         */
        mDrawerList.setItemChecked(position, true);
        setTitle(listArray[position]);

        //((ImageView)findViewById(R.id.image_view)).setBackgroundResource(R.drawable.image1);
    


drawer_list_item.xml:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:gravity="center_vertical"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:textColor="#fff"/>

还有我的 navigation_drawer_layout.xml:

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_
    android:layout_ >

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_
        android:layout_ />

    <ListView
        android:id="@+id/left_drawer"
        android:layout_
        android:layout_
        android:layout_gravity="start"
        android:background="#111"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />

</android.support.v4.widget.DrawerLayout>

这是与我正在尝试做的事情相关的全部代码。

当我按下HomeActivity 内的signin 按钮时,出现以下错误:

01-06 13:39:45.563  17197-17197/guide_me_for_all.guide_me_for_all E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: guide_me_for_all.guide_me_for_all, PID: 17197
    java.lang.RuntimeException: Unable to start activity ComponentInfoguide_me_for_all.guide_me_for_all/guide_me_for_all.guide_me_for_all.BaseActivity: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5146)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at guide_me_for_all.guide_me_for_all.BaseActivity.onCreate(BaseActivity.java:92)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5146)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
            at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

表示BaseActivity.java:92 就是这一行:getActionBar().setDisplayHomeAsUpEnabled(true); getActionBar() 在您的代码中为空,如果您的 min sdk 低于 11,您需要使用 getSupportActionBar(),如果 min sdk 大于 11 检查主题,也许您删除或隐藏它,如果这对你搜索 ``getActionBar()` return null` 没有帮助, 您是否在您的主题中使用了动作栏应用战斗?因为如果你这样做了,那么你需要扩展 ActionBarActivity 然后使用 getSupportActionBar().setDisplayHomeAsUpEnabled(true); 我的 minsdk 是 16。我打开了 styles.xmlMyActionBar 样式,parent="@android:style/Widget.Holo.Light.ActionBar" 有错误,它无法解析这个符号。是否有可能我删除了与样式和属性相关的重要内容?因为许多其他 xml 文件在 @android ... e.t.c 上存在问题,所以我只是删除了它们的样式。但我不知道如何在我的项目中再次导入这些样式。 【参考方案1】:

您可能想尝试以下方法:

public class BaseActivity extends Activity 

    protected FrameLayout frameLayout;
    protected ListView mDrawerList;
    protected String[] listArray =  "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" ;
    private static boolean isLaunch = true;
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle actionBarDrawerToggle;


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

        frameLayout = (FrameLayout)findViewById(R.id.content_frame);
        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, listArray));
        mDrawerList.setOnItemClickListener(new OnItemClickListener() 

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) 

                openActivity(position);
                getSupportActionBar().setTitle(listArray[position]);
            
        );

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

        actionBarDrawerToggle = new ActionBarDrawerToggle(
                this,                       /* host Activity */
                mDrawerLayout,              /* DrawerLayout object */
                R.drawable.ic_launcher,     /* nav drawer image to replace 'Up' caret */
                R.string.drawer_open,       /* "open drawer" description for accessibility */
                R.string.drawer_close)      /* "close drawer" description for accessibility */
        
            @Override
            public void onDrawerClosed(View drawerView) 
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                super.onDrawerClosed(drawerView);
            

            @Override
            public void onDrawerOpened(View drawerView) 
                //use the support library in App Combat
                getSupportActionBar().setTitle(getString(R.string.app_name));
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                super.onDrawerOpened(drawerView);
            

            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) 
                super.onDrawerSlide(drawerView, slideOffset);
            

            @Override
            public void onDrawerStateChanged(int newState) 
                super.onDrawerStateChanged(newState);
            
        ;
        mDrawerLayout.setDrawerListener(actionBarDrawerToggle);

        //in the line below you will always get the Selected position 0 because your isLaunch = true, so it will always give you this toast. If you think its unessecary remove it.
        if(isLaunch)
            isLaunch = false;
            openActivity(0);
        
    

由于 Navigation Drawer 可与 Fragments 一起使用,请尝试以下方法:

protected Fragment openActivity(int position) 

        mDrawerLayout.closeDrawer(mDrawerList);
        Fragment fragment = null;

        switch (position) 
            case 0:
                fragment =  new SearchActivity();
                break;
            
            case 1:
                fragment = new A();
                break;
            

            case 2:
                fragment = new B();
                break;
            

            case 3: 
                fragment = new C();
                break;
            
            case 4:
                fragment = new D();
                break;
            

        
        FragmentManager fm = getSupportFragmentManager();
        fm.beginTransaction().replace(R.id.content_frame, fragment).commit();
        return fragment;
    

像这样使用onOptionsItemSelected 方法:

@Override
public boolean onCreateOptionsMenu(Menu menu) 

    getMenuInflater().inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);


@Override
public boolean onOptionsItemSelected(MenuItem item) 


    if (actionBarDrawerToggle.onOptionsItemSelected(item)) 
        return true;
    
    return super.onOptionsItemSelected(item);

onBackPressed 方法如下:

删除else语句,因为您不想按返回按钮并打开抽屉

/* We can override onBackPressed method to toggle navigation drawer*/
@Override
public void onBackPressed() 
    if(mDrawerLayout.isDrawerOpen(mDrawerList))
        mDrawerLayout.closeDrawer(mDrawerList);
    

还将您的 SearchActivity 修改为片段,因为您希望在抽屉打开时成为第一个片段,如下所示:

public class SearchActivity extends Fragment

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
        View convertView = null;
        if(convertView == null)
            convertView= getLayoutInflater(savedInstanceState).inflate(R.layout.activity_search, container, false);
        
        return convertView;
    

希望对你有帮助!!!

【讨论】:

在onOptionsItemSelected我应该把这个放在最后吗?因为我得到一个错误,它没有回报。 return super.onOptionsItemSelected(item); 我刚刚又编辑了一件东西。而不是 getActionBar().setTitle(getString(R.string.app_name));使用 getSupportActionBar().setTitle(getString(R.string.app_name)); 一切正常。伟大和最重要的 EASY 解决方案。非常感谢!!【参考方案2】:

像这样在基本活动中覆盖 setContentView()。

@Override
public void setContentView(int layoutResID) 
    mDrawerLayout = (DrawerLayout) getLayoutInflater().inflate(
            R.layout.activity_base, null);
    mFrameLayout = (FrameLayout) mDrawerLayout
            .findViewById(R.id.frame_container);
    mDrawerList = (ExpandableListView) mDrawerLayout
            .findViewById(R.id.left_drawer);

    getLayoutInflater().inflate(layoutResID, mFrameLayout, true);
    super.setContentView(mDrawerLayout);

    //Your custom code

【讨论】:

我把它放在 cmets ` setContentView(R.layout.navigation_drawer_base_layout); ` 我在 onCreate 之外添加了您的代码,但更改了一些不同的名称。我没有添加任何其他代码。 部分错误:01-06 14:36:48.176 24882-24882/guide_me_for_all.guide_me_for_all E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: guide_me_for_all.guide_me_for_all, PID: 24882 java.lang.RuntimeException: Unable to start activity ComponentInfoguide_me_for_all.guide_me_for_all/guide_me_for_all.guide_me_for_all.BaseActivity: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271) ... 【参考方案3】:

你能打电话吗

setContentView(R.layout.navigation_drawer_layout);

而不是

setContentView(R.layout.navigation_drawer_base_layout);

BaseActivity?

此错误通常发生在您访问 findViewById 时出错。只需注释掉 mDrawerList.setItemChecked(position, true); 即可检查是否是问题所在。

【讨论】:

我没有布局:navigation_drawer_layout.xml。我还评论了您所说的那一行,但错误仍然存​​在:01-06 14:16:08.928 22231-22231/guide_me_for_all.guide_me_for_all E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: guide_me_for_all.guide_me_for_all, PID: 22231 java.lang.NullPointerException at guide_me_for_all.guide_me_for_all.BaseActivity.onPrepareOptionsMenu(BaseActivity.java:222) at android.app.Activity.onPreparePanel(Activity.java:2556) . . .

以上是关于具有多个活动的导航抽屉。 - 空指针异常的主要内容,如果未能解决你的问题,请参考以下文章

带有 ListView 的导航抽屉。 ListView 上的空指针异常

导航抽屉中的谷歌地图 - 空指针异常,哪个是正确的片段?

操作栏上不确定的进度条上的空指针异常

Navigation Drawer 方法收到 NullPointer 异常

Android 应用程序不断因空指针异常而崩溃

活动恢复时 ExpandableListAdapter getChildrenCount 空指针异常