导航Drawer中的Java.lang.NullPointerException

时间:2015-12-03 08:30:17

标签: java android nullpointerexception method-invocation android-navigation-drawer

我是android开发的新手,我正在尝试制作导航抽屉。一旦我将这些行放在我的java文件中,我的应用程序就会停止。有人能告诉我我的代码是做错了吗?它在构建项目时没有说什么,没有语法错误,但是当我将鼠标悬停在这两行中的一行时,它会说:

  

“getSupportActionBar.setHomeButtonEnabled中的方法调用可能会产生java.lang.NullPointerException”

getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

这是我的MainActivity.java文件:

import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.DrawableRes;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.widget.DrawerLayout;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;



public class MainActivity extends AppCompatActivity  implements AdapterView.OnItemClickListener {

    private ActionBarDrawerToggle actionBarDrawerToggle;
    private DrawerLayout drawerLayout;
    private ListView navList;

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



        drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
        actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,
                R.string.close_drawer,R.string.open_drawer);
        drawerLayout.setDrawerListener(actionBarDrawerToggle);


        //NullPointException in these two lines
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);


        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        navList = (ListView)findViewById(R.id.list_items);
        ArrayList<String> navArray = new ArrayList<String>();
        navArray.add("Home");
        navArray.add("Call Logs");
        navArray.add("Message Logs");
        navArray.add("Location");
        navArray.add("Browsing History");
        navArray.add("Dictionary");
        navArray.add("About Us");
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,navArray);
        navList.setAdapter(adapter);
        navList.setOnItemClickListener(this);

        loadSelection(0);


    }
    private void loadSelection(int i){
        navList.setItemChecked(i, true);
    }

    @Override
    public void onPostCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onPostCreate(savedInstanceState, persistentState);
        actionBarDrawerToggle.syncState();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        else if(id == android.R.id.home)
        {
            if(drawerLayout.isDrawerOpen(navList))
            {
                drawerLayout.closeDrawer(navList);
            }
            else
            {
                drawerLayout.openDrawer(navList);
            }
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch(position){
            case 0:

                break;
            case 1:

                break;
            case 2:

                break;
            case 3:

                break;
            case 4:

                break;
            case 5:

                break;
            case 6:

                break;
            case 7:

                break;
        }
        drawerLayout.closeDrawer(navList);

    }


    }

1 个答案:

答案 0 :(得分:0)

这是因为你不知道&#34; getSupportActionBar&#34;将返回&#34; ActionBar&#34;或&#34; null&#34;。

您有两种选择:

  • 您确定您将始终拥有一个ActionBar,因此您可以忽略此警告。
  • 您不确定或者您希望在没有警告的情况下拥有美学代码,因此您可以这样做:

    ActionBar actionBar = this.getSupportActionBar();
    if (actionBar != null) actionBar.setDisplayHomeAsUpEnabled(true);
    

顺便说一下,就像你可以看到here一样,你不需要&#34; setHomeButtonEnabled&#34;。