0

So, I am doing this project that includes an intro activity that switches to a menu activity with navigation drawer. Couple of days ago, I had an issue switching from intro to menu (Android Studio 1.0.2: java.lang.RuntimeException: Unable to start activity ComponentInfo: java.lang.NullPointerException) and it was solved by changing name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar" to name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar" thanks to goonerdroid's suggestion in my previous link. The issue that I have now is that I am trying to add a search action on the top right corner of the action bar alongside the navigation drawer so it can search on different future activities and javas that I will be adding. When I run the program, it enters the intro activity with no problems. When I click on the button to take me to the menuactivity, it displays the following error:

FATAL EXCEPTION: main
Process: com.wlodsgn.bunbunup, PID: 916
java.lang.NullPointerException
        at com.wlodsgn.bunbunup.MenuActivity.onPrepareOptionsMenu(MenuActivity.java:181)
        at android.app.Activity.onPreparePanel(Activity.java:2556)
        at android.support.v4.app.FragmentActivity.onPrepareOptionsPanel(FragmentActivity.java:469)
        at android.support.v7.app.ActionBarActivity.superOnPrepareOptionsPanel(ActionBarActivity.java:284)
        at android.support.v7.app.ActionBarActivityDelegate.onPrepareOptionsPanel(ActionBarActivityDelegate.java:204)
        at android.support.v7.app.ActionBarActivity.onPrepareOptionsPanel(ActionBarActivity.java:256)
        at android.support.v4.app.FragmentActivity.onPreparePanel(FragmentActivity.java:458)
        at android.support.v7.app.ActionBarActivity.superOnPreparePanel(ActionBarActivity.java:280)
        at android.support.v7.app.ActionBarActivityDelegate$1.onPreparePanel(ActionBarActivityDelegate.java:84)
        at android.support.v7.app.ActionBarActivityDelegateBase.preparePanel(ActionBarActivityDelegateBase.java:1006)
        at android.support.v7.app.ActionBarActivityDelegateBase.doInvalidatePanelMenu(ActionBarActivityDelegateBase.java:1182)
        at android.support.v7.app.ActionBarActivityDelegateBase.access$100(ActionBarActivityDelegateBase.java:79)
        at android.support.v7.app.ActionBarActivityDelegateBase$1.run(ActionBarActivityDelegateBase.java:118)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        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:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)

Here is my updated MenuActivity.java:

import java.util.ArrayList;

import android.support.v4.view.MenuItemCompat;
import android.support.v4.view.MenuItemCompat.OnActionExpandListener;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.SearchView.OnQueryTextListener;
import android.widget.TextView;
import android.widget.Toast;


import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

/**
 * Created by WiLo on 2/13/2015.
 */
public class MenuActivity extends ActionBarActivity implements OnQueryTextListener, OnActionExpandListener{

    private TextView texto;

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

    // nav drawer title
    private CharSequence mDrawerTitle;

    // used to store app title
    private CharSequence mTitle;

    // slide menu items
    private String[] navMenuTitles;
    private TypedArray navMenuIcons;

    private ArrayList<NavDrawerItem> navDrawerItems;
    private NavDrawerListAdapter adapter;

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

        texto = (TextView) findViewById(R.id.texto);

        mTitle = mDrawerTitle = getTitle();

        // load slide menu items
        navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);

        // nav drawer icons from resources
        navMenuIcons = getResources()
                .obtainTypedArray(R.array.nav_drawer_icons);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.list_slidermenu);

        navDrawerItems = new ArrayList<NavDrawerItem>();

        // agregar un nuevo item al menu deslizante
        // Favoritos
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
        // Pedidos
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
        // Catologo
        //navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1), true, "Estrenos"));
        // Contacto
        //navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));

        // Recycle the typed array
        navMenuIcons.recycle();

        mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

        // setting the nav drawer list adapter
        adapter = new NavDrawerListAdapter(getApplicationContext(),
                navDrawerItems);
        mDrawerList.setAdapter(adapter);

        // enabling action bar app icon and behaving it as toggle button
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, //nav menu toggle icon
                R.string.app_name, // nav drawer open - description for accessibility
                R.string.app_name // nav drawer close - description for accessibility
        ) {
            public void onDrawerClosed(View view) {
                getSupportActionBar().setTitle(mTitle);
                // calling onPrepareOptionsMenu() to show action bar icons
                invalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {
                getSupportActionBar().setTitle(mDrawerTitle);
                // calling onPrepareOptionsMenu() to hide action bar icons
                invalidateOptionsMenu();
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            // on first time display view for first nav item
            displayView(0);
        }
    }

    @Override
    public boolean onMenuItemActionExpand(MenuItem menuItem) {
        Toast.makeText(getApplicationContext(), "Abriendo Busqueda", Toast.LENGTH_SHORT).show();
        return true;
    }

    @Override
    public boolean onMenuItemActionCollapse(MenuItem menuItem) {
        Toast.makeText(getApplicationContext(), "Cerrando Busqueda", Toast.LENGTH_SHORT).show();
        return true;
    }

    @Override
    public boolean onQueryTextSubmit(String s) {
        texto.setText("Buscando...\n\n" + s);
        return false;
    }

    @Override
    public boolean onQueryTextChange(String s) {
        texto.setText("Que buscas? \n\n" + s);
        return false;
    }

    /**
     * Slide menu item click listener
     * */
    private class SlideMenuClickListener implements
            ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {
            // display view for selected nav drawer item
            displayView(position);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);

        MenuItem searchItem = menu.findItem(R.id.menu3_buscar);

        SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
        searchView.setOnQueryTextListener(this);

        MenuItemCompat.setOnActionExpandListener(searchItem, this);

        return super.onCreateOptionsMenu(menu);
    }

    /* *
     * Called when invalidateOptionsMenu() is triggered
     */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // if nav drawer is opened, hide the action items
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }

    /**
     * Diplaying fragment view for selected nav drawer list item
     * */
    private void displayView(int position) {
        // update the main content by replacing fragments
        Fragment fragment = null;
        switch (position) {
            case 0:
                fragment = new FmMenu();
                break;
            case 1:
                fragment = new FmContacto();
                break;

            default:
                break;
        }

        if (fragment != null) {
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.frame_container, fragment).commit();

            // update selected item and title, then close the drawer
            mDrawerList.setItemChecked(position, true);
            mDrawerList.setSelection(position);
            setTitle(navMenuTitles[position]);
            mDrawerLayout.closeDrawer(mDrawerList);
        } else {
            // error in creating fragment
            Log.e("BunBunUp", "MenuActivity - Error cuando se creo el fragment");
        }
    }

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

    /**
     * When using the ActionBarDrawerToggle, you must call it during
     * onPostCreate() and onConfigurationChanged()...
     */

    @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 toggls
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

}

Also my styles.xml:

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat">
    <!-- Customize your theme here. -->
</style>


</resources>

Updated IntroActivity.java

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;

/**
 * Created by WiLo on 2/13/2015.
 */
public class IntroActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
        /*getActionBar().hide();*/
        setContentView(R.layout.activity_intro);
        Log.i("BunBunUp", "MainActivity Created");
    }

    /**@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_intro, 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;
        }

        return super.onOptionsItemSelected(item);
    }**/

    public void startMenuActivity(View v){
        Intent intent = new Intent(IntroActivity.this, MenuActivity.class);
        startActivity(intent);
    }

    protected void onResume(){
        super.onResume();
        Log.i("BunBunUp", "IntroActivity Resumed");
    }

    protected void onPause(){
        super.onPause();
        Log.i("BunBunUp", "IntroActivity Paused");
    }

    protected void onStop(){
        super.onStop();
        Log.i("BunBunUp", "IntroActivity Stopped");
    }
}

If you need debug or any other info, let me know. Any help would be appreciated.

Community
  • 1
  • 1
w_lpz
  • 613
  • 4
  • 15
  • 28

1 Answers1

1

You are extending your MenuActivity to ActionBarActivity that's why it is asking you to use Theme.AppCompat. Its not about which api level you are targeting.Remember that ActionBarActivity is dependent to Theme.AppCompat . You cannot use it without Applying Theme.AppCompat.

Mohit
  • 2,608
  • 1
  • 22
  • 21
  • Thanks for the help, I did that and now its crashing from my `IntroActivity`. Now its showing me a java.lang.NullPointerException error. Going to update my post with my updated `IntroActivity` – w_lpz Feb 16 '15 at 03:10
  • What is your min api level. Update the error log also. – Mohit Feb 16 '15 at 03:20
  • Updated my post with the error log. The min API is set up starting from 15 – w_lpz Feb 16 '15 at 03:42
  • if you dont use the actionbar any more, then remove the line getActionBar().hide()...that is causing the error i think... – seba123neo Feb 16 '15 at 04:55
  • That is actually the code I have so the actionbar doesn't appear in the intro. Any other way or suggestion to do it? – w_lpz Feb 16 '15 at 05:08
  • Quick update, even though I added that part, just took out the `getActionBar().hide()` and you were right. For some reason, it solved the problem with the intro activity. But now the error is back in `MenuActivity`. Updating post with error log. – w_lpz Feb 16 '15 at 06:05