Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Carlos Fernando Gómez Carrero
Carlos Fernando Gómez Carrero
46,984 Points

Ribbit MainActivity onOptionsItemSelected is not working correctly

When I was trying to add extra credit to send a text message, I noticed that the menu is working strangely, whenever I select an option select another simultaneously. For example when I select action_edit_friends simultaneously select action_edit_friends, action_camera and action_msg_text

05-11 09:07:04.974: D/TEST IN MENU(2669): action_edit_friends
05-11 09:07:04.974: D/TEST IN MENU(2669): action_camera
05-11 09:07:04.986: D/TEST IN MENU(2669): action_msg_text

If I select me action_camera action_camera simultaneously select and action_msg_text

05-11 09:08:51.314: D/TEST IN MENU(2669): action_camera
05-11 09:08:51.322: D/TEST IN MENU(2669): action_msg_text

If I select only selects simultaneously action_logout action_logout, action_edit_friends, action_camera and action_msg_text

05-11 09:09:48.138: D/TEST IN MENU(2669): action_logout
05-11 09:09:48.178: D/TEST IN MENU(2669): action_edit_friends
05-11 09:09:48.178: D/TEST IN MENU(2669): action_camera
05-11 09:09:48.198: D/TEST IN MENU(2669): action_msg_text

And after select the action_logout gets an error:

05-11 09:09:48.282: E/AndroidRuntime(2669): FATAL EXCEPTION: main
05-11 09:09:48.282: E/AndroidRuntime(2669): Process: com.devcfgc.ribbit, PID: 2669
05-11 09:09:48.282: E/AndroidRuntime(2669): java.lang.RuntimeException: Unable to resume activity {com.devcfgc.ribbit/com.devcfgc.ribbit.EditFriendsActivity}: java.lang.NullPointerException
05-11 09:09:48.282: E/AndroidRuntime(2669):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2788)
05-11 09:09:48.282: E/AndroidRuntime(2669):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2817)
05-11 09:09:48.282: E/AndroidRuntime(2669):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
05-11 09:09:48.282: E/AndroidRuntime(2669):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-11 09:09:48.282: E/AndroidRuntime(2669):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-11 09:09:48.282: E/AndroidRuntime(2669):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-11 09:09:48.282: E/AndroidRuntime(2669):     at android.os.Looper.loop(Looper.java:136)
05-11 09:09:48.282: E/AndroidRuntime(2669):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-11 09:09:48.282: E/AndroidRuntime(2669):     at java.lang.reflect.Method.invokeNative(Native Method)
05-11 09:09:48.282: E/AndroidRuntime(2669):     at java.lang.reflect.Method.invoke(Method.java:515)
05-11 09:09:48.282: E/AndroidRuntime(2669):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-11 09:09:48.282: E/AndroidRuntime(2669):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-11 09:09:48.282: E/AndroidRuntime(2669):     at dalvik.system.NativeStart.main(Native Method)
05-11 09:09:48.282: E/AndroidRuntime(2669): Caused by: java.lang.NullPointerException
05-11 09:09:48.282: E/AndroidRuntime(2669):     at com.devcfgc.ribbit.EditFriendsActivity.onResume(EditFriendsActivity.java:48)
05-11 09:09:48.282: E/AndroidRuntime(2669):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
05-11 09:09:48.282: E/AndroidRuntime(2669):     at android.app.Activity.performResume(Activity.java:5310)
05-11 09:09:48.282: E/AndroidRuntime(2669):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2778)
05-11 09:09:48.282: E/AndroidRuntime(2669):     ... 12 more

Here is MainActivity

public class MainActivity extends FragmentActivity implements
        ActionBar.TabListener {

    private static final String TAG = MainActivity.class.getSimpleName();

    public static final int TAKE_PHOTO_REQUEST = 0;
    public static final int TAKE_VIDEO_REQUEST = 1;
    public static final int PICK_PHOTO_REQUEST = 2;
    public static final int PICK_VIDEO_REQUEST = 3;

    public static final int MEDIA_TYPE_IMAGE = 4;
    public static final int MEDIA_TYPE_VIDEO = 5;

    public static final int FILE_SIZE_LIMIT = 1024 * 1024 * 10; // 10 MB

    protected Uri mMediaUri;

    protected DialogInterface.OnClickListener mDialogListener = new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which) {
            case 0:// Take picture
                Intent takePhotoIntent = new Intent(
                        MediaStore.ACTION_IMAGE_CAPTURE);
                mMediaUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
                if (mMediaUri == null) {
                    // display an error
                    Toast.makeText(MainActivity.this,
                            R.string.error_external_storage, Toast.LENGTH_LONG)
                            .show();
                } else {
                    takePhotoIntent
                            .putExtra(MediaStore.EXTRA_OUTPUT, mMediaUri);
                    startActivityForResult(takePhotoIntent, TAKE_PHOTO_REQUEST);
                }

                break;

            case 1:// Take video
                Intent videoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
                mMediaUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
                if (mMediaUri == null) {
                    // display an error
                    Toast.makeText(MainActivity.this,
                            R.string.error_external_storage, Toast.LENGTH_LONG)
                            .show();
                } else {
                    videoIntent.putExtra(MediaStore.EXTRA_OUTPUT, mMediaUri);
                    videoIntent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 10);
                    videoIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);// 0=lowest_quality
                    startActivityForResult(videoIntent, TAKE_VIDEO_REQUEST);
                }

                break;

            case 2:// Choose picture
                Intent choosePhotoIntent = new Intent(Intent.ACTION_GET_CONTENT);
                choosePhotoIntent.setType("image/*");
                startActivityForResult(choosePhotoIntent, PICK_PHOTO_REQUEST);
                break;

            case 3:// Choose video
                Intent chooseVideoIntent = new Intent(Intent.ACTION_GET_CONTENT);
                chooseVideoIntent.setType("video/*");
                Toast.makeText(MainActivity.this,
                        R.string.video_file_size_warning, Toast.LENGTH_LONG)
                        .show();
                startActivityForResult(chooseVideoIntent, PICK_VIDEO_REQUEST);
                break;
            }
        }
    };

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
     * will keep every loaded fragment in memory. If this becomes too memory
     * intensive, it may be best to switch to a
     * {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    ViewPager mViewPager;

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

        // Analitycs from Parse
        ParseAnalytics.trackAppOpened(getIntent());

        ParseUser currentUser = ParseUser.getCurrentUser();
        if (currentUser == null) {
            navigateToLogin();
        } else {
            Log.i(TAG, currentUser.getUsername());
        }

        // Set up the action bar.
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the app.
        mSectionsPagerAdapter = new SectionsPagerAdapter(this,
                getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        // When swiping between different sections, select the corresponding
        // tab. We can also use ActionBar.Tab#select() to do this if we have
        // a reference to the Tab.
        mViewPager
                .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                        actionBar.setSelectedNavigationItem(position);
                    }
                });

        // For each of the sections in the app, add a tab to the action bar.
        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            // Create a tab with text corresponding to the page title defined by
            // the adapter. Also specify this Activity object, which implements
            // the TabListener interface, as the callback (listener) for when
            // this tab is selected.
            actionBar.addTab(actionBar.newTab()
                    .setText(mSectionsPagerAdapter.getPageTitle(i))
                    .setTabListener(this));
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {
            if (requestCode == PICK_PHOTO_REQUEST
                    || requestCode == PICK_VIDEO_REQUEST) {
                if (data == null) {
                    Toast.makeText(this, getString(R.string.general_error),
                            Toast.LENGTH_LONG).show();
                } else {
                    mMediaUri = data.getData();
                }

                Log.i(TAG, "Media URI: " + mMediaUri);
                if (requestCode == PICK_VIDEO_REQUEST) {
                    // make sure the file is less than 10 MB
                    int fileSize = 0;
                    InputStream inputStream = null;

                    try {
                        inputStream = getContentResolver().openInputStream(
                                mMediaUri);
                        fileSize = inputStream.available();
                    } catch (FileNotFoundException e) {
                        Toast.makeText(this, R.string.error_opening_file,
                                Toast.LENGTH_LONG).show();
                        return;
                    } catch (IOException e) {
                        Toast.makeText(this, R.string.error_opening_file,
                                Toast.LENGTH_LONG).show();
                        return;
                    } finally {
                        try {
                            inputStream.close();
                        } catch (IOException e) {/* Intenionally blank */
                        }
                    }

                    if (fileSize >= FILE_SIZE_LIMIT) {
                        Toast.makeText(this,
                                R.string.error_file_size_too_large,
                                Toast.LENGTH_LONG).show();
                        return;
                    }

                }

            } else {
                // Create a Broadcast to inform that the image is available
                Intent mediaScanIntent = new Intent(
                        Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                mediaScanIntent.setData(mMediaUri);
                sendBroadcast(mediaScanIntent);
            }

            // Start the RecipientsActivity to send the file to a friend
            Intent recipientsIntent = new Intent(this, RecipientsActivity.class);
            recipientsIntent.setData(mMediaUri);

            String fileType;
            if (requestCode == PICK_PHOTO_REQUEST
                    || requestCode == TAKE_PHOTO_REQUEST) {
                fileType = ParseConstants.TYPE_IMAGE;
            } else {
                fileType = ParseConstants.TYPE_VIDEO;
            }
            recipientsIntent.putExtra(ParseConstants.KEY_FILE_TYPE, fileType);

            startActivity(recipientsIntent);

        } else if (resultCode != RESULT_CANCELED) {
            Toast.makeText(this, R.string.general_error, Toast.LENGTH_LONG)
                    .show();
        }
    }

    private void navigateToLogin() {
        Intent intent = new Intent(this, LoginActivity.class);
        // With this the back button from the login page, close the activity
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }

    @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) {
        int itemId = item.getItemId();

        switch (itemId) {
        case R.id.action_logout:
            Log.d("TEST IN MENU", "action_logout");
            ParseUser.logOut();
            navigateToLogin();
        case R.id.action_edit_friends:
            Log.d("TEST IN MENU", "action_edit_friends");
            Intent intent = new Intent(this, EditFriendsActivity.class);
            startActivity(intent);
        case R.id.action_camera:
            Log.d("TEST IN MENU", "action_camera");
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setItems(R.array.camera_choices, mDialogListener);
            AlertDialog dialog = builder.create();
            dialog.show();

        case R.id.action_msg_text:
            Log.d("TEST IN MENU", "action_msg_text");
            Toast.makeText(this, "TEST", Toast.LENGTH_LONG).show();
            /*
             * Intent intentTextMsg = new Intent(this, TextMsgActivity.class);
             * startActivity(intentTextMsg);
             */
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, switch to the corresponding page in
        // the ViewPager.
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {
    }

    private Uri getOutputMediaFileUri(int mediaType) {
        // To be safe, you should check that the SDCard or ExternalStore is
        // mounted
        // using Environment.getExternalStorageState() before doing this.
        if (isExternalStorageAvailable()) {
            // get the URI

            // 1. Get the external storage directory
            String appName = MainActivity.this.getString(R.string.app_name);
            File mediaStorageDir = new File(
                    Environment
                            .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                    appName);

            // 2. Create our subdirectory
            if (!mediaStorageDir.exists()) {
                if (!mediaStorageDir.mkdirs()) {
                    Log.e(TAG, "Failed to create directory.");
                    return null;
                }
            }

            // 3. Create a file name
            // 4. Create the file
            File mediaFile;
            Date now = new Date();
            String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                    Locale.US).format(now);

            String path = mediaStorageDir.getPath() + File.separator;
            if (mediaType == MEDIA_TYPE_IMAGE) {
                mediaFile = new File(path + "IMG_" + timestamp + ".jpg");
            } else if (mediaType == MEDIA_TYPE_VIDEO) {
                mediaFile = new File(path + "VID_" + timestamp + ".mp4");
            } else {
                return null;
            }

            Log.d(TAG, "File: " + Uri.fromFile(mediaFile));

            // 5. Return the file's URI
            return Uri.fromFile(mediaFile);
        } else {
            return null;
        }
    }

    private boolean isExternalStorageAvailable() {
        String state = Environment.getExternalStorageState();
        if (state.equals(Environment.MEDIA_MOUNTED)) {
            return true;
        } else {
            return false;
        }
    }
}

And EditFriends

public class EditFriendsActivity extends ListActivity {

    public static final String TAG = EditFriendsActivity.class.getSimpleName();

    protected List<ParseUser> mUsers;
    protected ParseRelation<ParseUser> mFriendsRelation;
    protected ParseUser mCurrentUser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.activity_edit_friends);
        // Show the Up button in the action bar.
        setupActionBar();

        getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    }

    @Override
    protected void onResume() {
        super.onResume();

        mCurrentUser = ParseUser.getCurrentUser();
        mFriendsRelation = mCurrentUser
                .getRelation(ParseConstants.KEY_FRIENDS_RELATION);

        setProgressBarIndeterminateVisibility(true);

        ParseQuery<ParseUser> query = ParseUser.getQuery();
        query.orderByAscending(ParseConstants.KEY_USERNAME);
        query.setLimit(1000);
        query.findInBackground(new FindCallback<ParseUser>() {
            @Override
            public void done(List<ParseUser> users, ParseException e) {
                setProgressBarIndeterminateVisibility(false);

                if (e == null) {
                    // Success
                    mUsers = users;
                    String[] usernames = new String[mUsers.size()];
                    int i = 0;
                    for (ParseUser user : mUsers) {
                        usernames[i] = user.getUsername();
                        i++;
                    }
                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                            EditFriendsActivity.this,
                            android.R.layout.simple_list_item_checked,
                            usernames);
                    setListAdapter(adapter);

                    addFriendCheckMarks();
                } else {
                    Log.e(TAG, e.getMessage());
                    AlertDialog.Builder builder = new AlertDialog.Builder(
                            EditFriendsActivity.this);
                    builder.setMessage(e.getMessage())
                            .setTitle(R.string.error_title)
                            .setPositiveButton(android.R.string.ok, null);
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }
            }
        });
    }

    /**
     * Set up the {@link android.app.ActionBar}.
     */
    private void setupActionBar() {

        getActionBar().setDisplayHomeAsUpEnabled(true);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            // This ID represents the Home or Up button. In the case of this
            // activity, the Up button is shown. Use NavUtils to allow users
            // to navigate up one level in the application structure. For
            // more details, see the Navigation pattern on Android Design:
            //
            // http://developer.android.com/design/patterns/navigation.html#up-vs-back
            //
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        if (getListView().isItemChecked(position)) {
            // add friend
            mFriendsRelation.add(mUsers.get(position));
        } else {
            // remove friend
            mFriendsRelation.remove(mUsers.get(position));
        }

        mCurrentUser.saveInBackground(new SaveCallback() {

            @Override
            public void done(ParseException e) {
                if (e != null) {
                    Log.e(TAG, e.getMessage());
                }
            }
        });
    }

    private void addFriendCheckMarks() {
        mFriendsRelation.getQuery().findInBackground(
                new FindCallback<ParseUser>() {

                    @Override
                    public void done(List<ParseUser> friends, ParseException e) {
                        if (e == null) {
                            // list returned - look for a match
                            for (int i = 0; i < mUsers.size(); i++) {
                                ParseUser user = mUsers.get(i);
                                for (ParseUser friend : friends) {
                                    if (friend.getObjectId().equals(
                                            user.getObjectId())) {
                                        getListView().setItemChecked(i, true);
                                    }
                                }
                            }
                        } else {
                            Log.e(TAG, e.getMessage());
                        }
                    }
                });
    }

}
Carlos Fernando Gómez Carrero
Carlos Fernando Gómez Carrero
46,984 Points

I downloaded the files project of Ribbit from the module "Retrieving and Viewing Messages" and has the same issue.... is normal???

The problem can be seen in the method onOptionsItemSelected, the different options get called simultaneously

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();

        switch (itemId) {
        case R.id.action_logout:
            Log.d("TEST IN MENU", "action_logout");
            ParseUser.logOut();
            navigateToLogin();
        case R.id.action_edit_friends:
            Log.d("TEST IN MENU", "action_edit_friends");
            Intent intent = new Intent(this, EditFriendsActivity.class);
            startActivity(intent);
        case R.id.action_camera:
            Log.d("TEST IN MENU", "action_camera");
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setItems(R.array.camera_choices, mDialogListener);
            AlertDialog dialog = builder.create();
            dialog.show();

        case R.id.action_msg_text:
            Log.d("TEST IN MENU", "action_msg_text");
            Toast.makeText(this, "TEST", Toast.LENGTH_LONG).show();
            /*
             * Intent intentTextMsg = new Intent(this, TextMsgActivity.class);
             * startActivity(intentTextMsg);
             */
        }

2 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Yep, you found a bug! :) I realized my mistake and correct it in the next video, I think. I forgot the break; statements in the switch statement of the onOptionsItemSelected() method. We need to add breaks to break out of the switch, or else all the options get triggered.

Carlos Fernando Gómez Carrero
Carlos Fernando Gómez Carrero
46,984 Points

Great, now everything works perfectly!!! I was going crazy thinking that the problem was somewhere else. Thanks.