导航抽屉没有启动器活动

时间:2014-03-29 22:45:44

标签: android nullpointerexception navigation-drawer

我的导航抽屉在MainActivity中实现。如果我使用MainAktivity作为发射器,它可以完美运行。但是,当我使用另一个活动作为LAUNCHER并从另一个活动运行MainActivity时,我的应用程序崩溃了。请查看我的Manifest XML。谢谢!

此版本有效:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.examples.pk"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="android.permission.INTERNET" >
    </uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
    </uses-permission>

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:hardwareAccelerated="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.examples.pk.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.examples.pk.Introduction"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.INTRODUCTION" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.examples.pk.SetAccess"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="com.examples.pk.SETACCESS" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.pk.LogIn"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="com.examples.pk.LOGIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <service android:name=".ConnectionService" >
        </service>
    </application>
</manifest>

这个版本不起作用:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.examples.pk"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="android.permission.INTERNET" >
    </uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
    </uses-permission>

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:hardwareAccelerated="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.examples.pk.Introduction"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.examples.pk.SetAccess"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="com.examples.pk.SETACCESS" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.pk.LogIn"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="com.examples.pk.LOGIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.examples.pk.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAINACTIVITY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <service android:name=".ConnectionService" >
        </service>
    </application>
</manifest>

错误日志:

03-29 22:38:51.755: E/AndroidRuntime(1331): FATAL EXCEPTION: main
03-29 22:38:51.755: E/AndroidRuntime(1331): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pk/com.example.pk.MainActivity}: java.lang.NullPointerException
03-29 22:38:51.755: E/AndroidRuntime(1331):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-29 22:38:51.755: E/AndroidRuntime(1331):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-29 22:38:51.755: E/AndroidRuntime(1331):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-29 22:38:51.755: E/AndroidRuntime(1331):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-29 22:38:51.755: E/AndroidRuntime(1331):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-29 22:38:51.755: E/AndroidRuntime(1331):     at android.os.Looper.loop(Looper.java:137)
03-29 22:38:51.755: E/AndroidRuntime(1331):     at android.app.ActivityThread.main(ActivityThread.java:5041)
03-29 22:38:51.755: E/AndroidRuntime(1331):     at java.lang.reflect.Method.invokeNative(Native Method)
03-29 22:38:51.755: E/AndroidRuntime(1331):     at java.lang.reflect.Method.invoke(Method.java:511)
03-29 22:38:51.755: E/AndroidRuntime(1331):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-29 22:38:51.755: E/AndroidRuntime(1331):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-29 22:38:51.755: E/AndroidRuntime(1331):     at dalvik.system.NativeStart.main(Native Method)
03-29 22:38:51.755: E/AndroidRuntime(1331): Caused by: java.lang.NullPointerException
03-29 22:38:51.755: E/AndroidRuntime(1331):     at com.example.pk.MainActivity.onCreate(MainActivity.java:75)
03-29 22:38:51.755: E/AndroidRuntime(1331):     at android.app.Activity.performCreate(Activity.java:5104)
03-29 22:38:51.755: E/AndroidRuntime(1331):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-29 22:38:51.755: E/AndroidRuntime(1331):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-29 22:38:51.755: E/AndroidRuntime(1331):     ... 11 more

已更新 MainActivity:

package com.examples.pk;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
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.ListView;

public class MainActivity extends Activity {

    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

    private CharSequence mDrawerTitle;
    private CharSequence mTitle;
    CustomDrawerAdapter adapter;

    List<DrawerItem> dataList;

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

        // Initializing
        dataList = new ArrayList<DrawerItem>();
        mTitle = mDrawerTitle = getTitle();
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
                GravityCompat.START);

        // Add Drawer Item to dataList
        dataList.add(new DrawerItem("Message", R.drawable.ic_action_email));
        dataList.add(new DrawerItem("Likes", R.drawable.ic_action_good));
        dataList.add(new DrawerItem("Games", R.drawable.ic_action_gamepad));
        dataList.add(new DrawerItem("Lables", R.drawable.ic_action_labels));
        dataList.add(new DrawerItem("Search", R.drawable.ic_action_search));
        dataList.add(new DrawerItem("Cloud", R.drawable.ic_action_cloud));
        dataList.add(new DrawerItem("Camara", R.drawable.ic_action_camera));
        dataList.add(new DrawerItem("Video", R.drawable.ic_action_video));
        dataList.add(new DrawerItem("Groups", R.drawable.ic_action_group));
        dataList.add(new DrawerItem("Import & Export",
                R.drawable.ic_action_import_export));
        dataList.add(new DrawerItem("About", R.drawable.ic_action_about));
        dataList.add(new DrawerItem("Settings", R.drawable.ic_action_settings));
        dataList.add(new DrawerItem("Help", R.drawable.ic_action_help));

        adapter = new CustomDrawerAdapter(this, R.layout.custom_drawer_item,
                dataList);

        mDrawerList.setAdapter(adapter);

        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

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

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, R.string.drawer_open,
                R.string.drawer_close) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(mTitle);
                invalidateOptionsMenu(); // creates call to
                                            // onPrepareOptionsMenu()
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(mDrawerTitle);
                invalidateOptionsMenu(); // creates call to
                                            // onPrepareOptionsMenu()
            }
        };

        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            SelectItem(0);
        }

    }

    @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;
    }

    public void SelectItem(int possition) {

        Fragment fragment = null;
        Bundle args = new Bundle();
        switch (possition) {
        case 0:
            fragment = new FragmentOne();
            args.putString(FragmentOne.ITEM_NAME, dataList.get(possition)
                    .getItemName());
            args.putInt(FragmentOne.IMAGE_RESOURCE_ID, dataList.get(possition)
                    .getImgResID());
            break;
        case 1:
            fragment = new FragmentTwo();
            args.putString(FragmentTwo.ITEM_NAME, dataList.get(possition)
                    .getItemName());
            args.putInt(FragmentTwo.IMAGE_RESOURCE_ID, dataList.get(possition)
                    .getImgResID());
            break;
        case 2:
            fragment = new FragmentThree();
            args.putString(FragmentThree.ITEM_NAME, dataList.get(possition)
                    .getItemName());
            args.putInt(FragmentThree.IMAGE_RESOURCE_ID, dataList
                    .get(possition).getImgResID());
            break;
        case 3:
            fragment = new FragmentOne();
            args.putString(FragmentOne.ITEM_NAME, dataList.get(possition)
                    .getItemName());
            args.putInt(FragmentOne.IMAGE_RESOURCE_ID, dataList.get(possition)
                    .getImgResID());
            break;
        case 4:
            fragment = new FragmentTwo();
            args.putString(FragmentTwo.ITEM_NAME, dataList.get(possition)
                    .getItemName());
            args.putInt(FragmentTwo.IMAGE_RESOURCE_ID, dataList.get(possition)
                    .getImgResID());
            break;
        case 5:
            fragment = new FragmentThree();
            args.putString(FragmentThree.ITEM_NAME, dataList.get(possition)
                    .getItemName());
            args.putInt(FragmentThree.IMAGE_RESOURCE_ID, dataList
                    .get(possition).getImgResID());
            break;
        case 6:
            fragment = new FragmentOne();
            args.putString(FragmentOne.ITEM_NAME, dataList.get(possition)
                    .getItemName());
            args.putInt(FragmentOne.IMAGE_RESOURCE_ID, dataList.get(possition)
                    .getImgResID());
            break;
        case 7:
            fragment = new FragmentTwo();
            args.putString(FragmentTwo.ITEM_NAME, dataList.get(possition)
                    .getItemName());
            args.putInt(FragmentTwo.IMAGE_RESOURCE_ID, dataList.get(possition)
                    .getImgResID());
            break;
        case 8:
            fragment = new FragmentThree();
            args.putString(FragmentThree.ITEM_NAME, dataList.get(possition)
                    .getItemName());
            args.putInt(FragmentThree.IMAGE_RESOURCE_ID, dataList
                    .get(possition).getImgResID());
            break;
        case 9:
            fragment = new FragmentOne();
            args.putString(FragmentOne.ITEM_NAME, dataList.get(possition)
                    .getItemName());
            args.putInt(FragmentOne.IMAGE_RESOURCE_ID, dataList.get(possition)
                    .getImgResID());
            break;
        case 10:
            fragment = new FragmentTwo();
            args.putString(FragmentTwo.ITEM_NAME, dataList.get(possition)
                    .getItemName());
            args.putInt(FragmentTwo.IMAGE_RESOURCE_ID, dataList.get(possition)
                    .getImgResID());
            break;
        case 11:
            fragment = new FragmentThree();
            args.putString(FragmentThree.ITEM_NAME, dataList.get(possition)
                    .getItemName());
            args.putInt(FragmentThree.IMAGE_RESOURCE_ID, dataList
                    .get(possition).getImgResID());
            break;
        case 12:
            fragment = new FragmentOne();
            args.putString(FragmentOne.ITEM_NAME, dataList.get(possition)
                    .getItemName());
            args.putInt(FragmentOne.IMAGE_RESOURCE_ID, dataList.get(possition)
                    .getImgResID());
            break;
        default:
            break;
        }

        fragment.setArguments(args);
        FragmentManager frgManager = getFragmentManager();
        frgManager.beginTransaction().replace(R.id.content_frame, fragment)
                .commit();

        mDrawerList.setItemChecked(possition, true);
        setTitle(dataList.get(possition).getItemName());
        mDrawerLayout.closeDrawer(mDrawerList);

    }

    @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
        getActionBar().setTitle(mTitle);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggles
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @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 (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        return false;
    }

    private class DrawerItemClickListener implements
            ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            SelectItem(position);

        }
    }

}

0 个答案:

没有答案