NullPointerException navigationDrawer和android中的选项卡

时间:2015-06-20 15:15:02

标签: android tabs navigation-drawer

我尝试在中创建navigationDrawer和标签,但得到此异常:

 06-20 17:10:35.717: E/AndroidRuntime(17235): FATAL EXCEPTION: main
06-20 17:10:35.717: E/AndroidRuntime(17235): Process: com.dusandimitrijevic.spisakzakupovinu, PID: 17235
06-20 17:10:35.717: E/AndroidRuntime(17235): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dusandimitrijevic.spisakzakupovinu/com.dusandimitrijevic.spisakzakupovinu.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
06-20 17:10:35.717: E/AndroidRuntime(17235):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
06-20 17:10:35.717: E/AndroidRuntime(17235):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2760)
06-20 17:10:35.717: E/AndroidRuntime(17235):    at android.app.ActivityThread.access$900(ActivityThread.java:177)
06-20 17:10:35.717: E/AndroidRuntime(17235):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
06-20 17:10:35.717: E/AndroidRuntime(17235):    at android.os.Handler.dispatchMessage(Handler.java:102)
06-20 17:10:35.717: E/AndroidRuntime(17235):    at android.os.Looper.loop(Looper.java:145)
06-20 17:10:35.717: E/AndroidRuntime(17235):    at android.app.ActivityThread.main(ActivityThread.java:5944)
06-20 17:10:35.717: E/AndroidRuntime(17235):    at java.lang.reflect.Method.invoke(Native Method)
06-20 17:10:35.717: E/AndroidRuntime(17235):    at java.lang.reflect.Method.invoke(Method.java:372)
06-20 17:10:35.717: E/AndroidRuntime(17235):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
06-20 17:10:35.717: E/AndroidRuntime(17235):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
06-20 17:10:35.717: E/AndroidRuntime(17235): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
06-20 17:10:35.717: E/AndroidRuntime(17235):    at com.dusandimitrijevic.spisakzakupovinu.MainActivity.addDrawerItems(MainActivity.java:84)
06-20 17:10:35.717: E/AndroidRuntime(17235):    at com.dusandimitrijevic.spisakzakupovinu.MainActivity.onCreate(MainActivity.java:55)
06-20 17:10:35.717: E/AndroidRuntime(17235):    at android.app.Activity.performCreate(Activity.java:6289)
06-20 17:10:35.717: E/AndroidRuntime(17235):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
06-20 17:10:35.717: E/AndroidRuntime(17235):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
06-20 17:10:35.717: E/AndroidRuntime(17235):    ... 10 more

这里是MainActivity的代码:

    package com.dusandimitrijevic.spisakzakupovinu;



import com.dusandimitrijevic.fragments.FragmentMojSpisak;
import com.dusandimitrijevic.fragments.FragmentProizvodi;
import com.dusandimitrijevic.fragments.FragmentSearch;
import com.tabs.dusandimitrijevic.SlidingTabLayout;

import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ImageSpan;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {


    private ActionBarDrawerToggle mDrawerToggle;
    private DrawerLayout mDrawerLayout;
    private String mActivityTitle;
    private ListView mDrawerList;
    private ArrayAdapter<String> mAdapter;
    private Toolbar toolbar;
    private ViewPager mPager;
    private SlidingTabLayout mTabs;
    private static final int MOJ_SPISAK = 0;
    private static final int PROIZVODI = 1;
    private static final int PROIZVODI_PRETRAGA = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 

        addDrawerItems();
        setupDrawer();

        mDrawerList = (ListView)findViewById(R.id.navList);
        toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setIcon(R.drawable.ic_launcher);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        //podešavanja za navigationDrawer
        mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
        mActivityTitle = getTitle().toString();

        //podešavanja za tabove
        mPager = (ViewPager) findViewById(R.id.pager);
        mPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
        mTabs = (SlidingTabLayout) findViewById(R.id.tabs);
        mTabs.setCustomTabView(R.layout.custom_tab_view, R.id.tabText);
        mTabs.setDistributeEvenly(true);
        mTabs.setBackgroundColor(getResources().getColor(R.color.primaryColor));
        mTabs.setSelectedIndicatorColors(getResources().getColor(R.color.accentColor));
        mTabs.setViewPager(mPager);

    }

    private void addDrawerItems() {
        String[] osArray = { "Android", "iOS", "Windows", "OS X", "Linux" };
        mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, osArray);
        mDrawerList.setAdapter(mAdapter);

        mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, "Time for an upgrade!", Toast.LENGTH_SHORT).show();
            }
        });
    }



    private void setupDrawer() {
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.string.drawer_open, R.string.drawer_close) {

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                getSupportActionBar().setTitle("Navigation!");
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                getSupportActionBar().setTitle(mActivityTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

        };

        mDrawerToggle.setDrawerIndicatorEnabled(true);
        mDrawerLayout.setDrawerListener(mDrawerToggle);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.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();

        if (id == R.id.action_settings) {
            Toast.makeText(MainActivity.this, "Ovo su podešavanja", Toast.LENGTH_SHORT).show();
            return true;
        }
        if (id == R.id.action_about) { 
            Toast.makeText(MainActivity.this, "O aplikaciji", Toast.LENGTH_SHORT).show();
            return true;
        }
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    class MyPagerAdapter extends FragmentStatePagerAdapter{


        int icons[] = {R.drawable.ic_action,R.drawable.ic_action_1, R.drawable.ic_actio_2 };
        String[] tabs;


        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
            tabs=getResources().getStringArray(R.array.tabs);
        }

        @Override
        public Fragment getItem(int num) {
            Fragment fragment = null;
            switch(num){
                case MOJ_SPISAK:
                    fragment = FragmentMojSpisak.newInstance("", "");
                    break;
                case PROIZVODI:
                    fragment = FragmentProizvodi.newInstance("", "");
                    break;
                case PROIZVODI_PRETRAGA:
                    fragment = FragmentSearch.newInstance("", "");
                    break;          
            }
            return fragment;

        }

        @Override
        public CharSequence getPageTitle(int position) {


            Drawable drawable = getDrawable(icons[position]);
            drawable.setBounds(0, 0, 90, 90);
            ImageSpan imageSpan = new ImageSpan(drawable);
            SpannableString spannableString = new SpannableString(" ");
            spannableString.setSpan(imageSpan, 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            return spannableString;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return 3;
        }       
    }
}

这是我的主要activity.xml文件,如何使我的导航抽屉不超过操作栏,然后在它下面?

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

    <LinearLayout   android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    tools:context="com.dusandimitrijevic.spisakzakupovinu.MainActivity" >

        <include
            android:id="@+id/app_bar"
            layout="@layout/app_bar" />

        <com.tabs.dusandimitrijevic.SlidingTabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:elevation="2dp"
            android:background="@color/primaryColor"/>

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_height="0dp"
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:background="@android:color/white"
            ></android.support.v4.view.ViewPager>

    </LinearLayout>

    <ListView
        android:id="@+id/navList"
        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:layout_gravity="left|start"
        android:background="#ffeeeeee"/>

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

1 个答案:

答案 0 :(得分:0)

onCreate中,您在为addDrawerItems()分配值之前呼叫mDrawerList。因此,当您尝试设置其适配器时它为null。