Autore Topic: Problema con Navigation Drawer  (Letto 321 volte)

Offline Fenicexx

  • Nuovo arrivato
  • *
  • Post: 25
  • Respect: 0
    • Mostra profilo
Problema con Navigation Drawer
« il: 29 Giugno 2015, 08:41:40 CEST »
0
Ciao,
sto seguendo questa guida http://www.anddev.it/index.php/topic,11160.0.html[/url]

Però mi restituisce un errore. Questo è ilLogCat:

Codice: [Seleziona]
06-29 08:27:36.053: E/AndroidRuntime(13284): FATAL EXCEPTION: main
06-29 08:27:36.053: E/AndroidRuntime(13284): java.lang.RuntimeException: Unable to start activity ComponentInfo{it.gc.prova/it.gc.prova.FirstPageActivity}: java.lang.NullPointerException
06-29 08:27:36.053: E/AndroidRuntime(13284):         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
06-29 08:27:36.053: E/AndroidRuntime(13284):         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
06-29 08:27:36.053: E/AndroidRuntime(13284):         at android.app.ActivityThread.access$700(ActivityThread.java:140)
06-29 08:27:36.053: E/AndroidRuntime(13284):         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
06-29 08:27:36.053: E/AndroidRuntime(13284):         at android.os.Handler.dispatchMessage(Handler.java:99)
06-29 08:27:36.053: E/AndroidRuntime(13284):         at android.os.Looper.loop(Looper.java:137)
06-29 08:27:36.053: E/AndroidRuntime(13284):         at android.app.ActivityThread.main(ActivityThread.java:4921)
06-29 08:27:36.053: E/AndroidRuntime(13284):         at java.lang.reflect.Method.invokeNative(Native Method)
06-29 08:27:36.053: E/AndroidRuntime(13284):         at java.lang.reflect.Method.invoke(Method.java:511)
06-29 08:27:36.053: E/AndroidRuntime(13284):         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
06-29 08:27:36.053: E/AndroidRuntime(13284):         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
06-29 08:27:36.053: E/AndroidRuntime(13284):         at dalvik.system.NativeStart.main(Native Method)
06-29 08:27:36.053: E/AndroidRuntime(13284): Caused by: java.lang.NullPointerException
06-29 08:27:36.053: E/AndroidRuntime(13284):         at it.gc.prova.FirstPageActivity.onCreate(FirstPageActivity.java:32)
06-29 08:27:36.053: E/AndroidRuntime(13284):         at android.app.Activity.performCreate(Activity.java:5206)
06-29 08:27:36.053: E/AndroidRuntime(13284):         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
06-29 08:27:36.053: E/AndroidRuntime(13284):         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
06-29 08:27:36.053: E/AndroidRuntime(13284):         ... 11 more

Questa è l'acitivity dove sto provando il navigation drawer:

Codice (Java): [Seleziona]
public class FirstPageActivity extends Activity {

        private ListView mDrawerList;
        private DrawerLayout mDrawer;
        private CustomActionBarDrawerToggle mDrawerToggle;
        private String[] menuItems;

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

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

                mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);

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

                _initMenu();
                mDrawerToggle = new CustomActionBarDrawerToggle(this, mDrawer);
                mDrawer.setDrawerListener(mDrawerToggle);

        }

        private void _initMenu() {
                NsMenuAdapter mAdapter = new NsMenuAdapter(this);

                // Add Header
                mAdapter.addHeader(R.string.ns_menu_main_header);

                // Add first block

                menuItems = getResources().getStringArray(
                                R.array.ns_menu_items);
                String[] menuItemsIcon = getResources().getStringArray(
                                R.array.ns_menu_items_icon);

                int res = 0;
                for (String item : menuItems) {

                        int id_title = getResources().getIdentifier(item, "string",
                                        this.getPackageName());
                        int id_icon = getResources().getIdentifier(menuItemsIcon[res],
                                        "drawable", this.getPackageName());

                        NsMenuItemModel mItem = new NsMenuItemModel(id_title, id_icon);
                        if (res==1) mItem.counter=12; //it is just an example...
                        if (res==3) mItem.counter=3; //it is just an example...
                        mAdapter.addItem(mItem);
                        res++;
                }

                mAdapter.addHeader(R.string.ns_menu_main_header2);

                mDrawerList = (ListView) findViewById(R.id.drawer);
                if (mDrawerList != null)
                        mDrawerList.setAdapter(mAdapter);
                 
                mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        }

        @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);
                mDrawerToggle.onConfigurationChanged(newConfig);
        }
       
        @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.policlinico, menu);
        return super.onCreateOptionsMenu(menu);
    }

    /* 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 = mDrawer.isDrawerOpen(mDrawerList);
        menu.findItem(R.id.action_save).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
                /*
                 * The action bar home/up should open or close the drawer.
                 * ActionBarDrawerToggle will take care of this.
                 */

                if (mDrawerToggle.onOptionsItemSelected(item)) {
                        return true;
                }

                // Handle your other action bar items...
                return super.onOptionsItemSelected(item);
        }

        private class CustomActionBarDrawerToggle extends ActionBarDrawerToggle {

                public CustomActionBarDrawerToggle(Activity mActivity,DrawerLayout mDrawerLayout){
                        super(
                            mActivity,
                            mDrawerLayout,
                            R.drawable.ic_drawer,
                            R.string.ns_menu_open);
                }

                @Override
                public void onDrawerClosed(View view) {
                        getActionBar().setTitle(getString(R.string.ns_menu_close));
                        invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                }

                @Override
                public void onDrawerOpened(View drawerView) {
                        getActionBar().setTitle(getString(R.string.ns_menu_open));
                        invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                }
        }
       
        private class DrawerItemClickListener implements ListView.OnItemClickListener {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {
                        // Highlight the selected item, update the title, and close the drawer
                        // update selected item and title, then close the drawer
                mDrawerList.setItemChecked(position, true);
                String text= "menu click... should be implemented";
                Toast.makeText(FirstPageActivity.this, text , Toast.LENGTH_LONG).show();
                //You should reset item counter
                mDrawer.closeDrawer(mDrawerList);
                       
                }
               
        }

}

E questo è layout (gli altri xml sono uguali alla guida):
Codice (XML): [Seleziona]
<android.support.v4.widget.DrawerLayout
  xmlns:android="[url]http://schemas.android.com/apk/res/android"
  android:id="@+id/drawer_layout"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >
   
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context="${relativePackage}.${activityClass}" >

    <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" />
   
    <!-- Navigation Drawer -->
    <ListView
      android:id="@+id/drawer"
      android:layout_width="320dp"
      android:layout_height="match_parent"
      android:layout_gravity="start"
      android:background="#F3F3F4"
      android:choiceMode="singleChoice"
      android:divider="@android:color/transparent"
      android:dividerHeight="0dp" ></ListView>

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

Infine, questo è il manifest:
Codice (XML): [Seleziona]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="it.gc.prova"
   android:versionCode="1"
   android:versionName="1.0" >

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

    <application
       android:allowBackup="true"
       android:icon="@drawable/ic_launcher"
       android:label="@string/app_name"
       android:theme="@style/AppTheme" >
        <activity
           android:name=".Prova"
           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=".FirstPageActivity"
           android:label="@string/title_activity_first_page" >
        </activity>
        <activity
           android:name="it.gc.prova.utils.Orientation"
           android:label="@string/title_activity_orientation" >
        </activity>
        <activity
           android:name="it.gc.prova.model.NsMenuAdapter"
           android:label="@string/title_activity_ns_menu_adapter" >
        </activity>
    </application>

</manifest>

Spero riusciate ad aiutarmi a risolvere, sono bloccato da un po' di giorni.

Grazie mille