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

Eyal Cohen
PLUS
Eyal Cohen
Courses Plus Student 438 Points

I've entered the ParseUser.logout(), the application keeps on being stuck with the current screen. How it can be fixed?

Project: TreehouseWorkshop_START

Class: MainFeedActivity.java Method: public boolean onOptionsItemSelected(MenuItem item)

--> case R.id.logoutButton: /** Log current user out using ParseUser.logOut() */ ParseUser.logOut();

Intent intent = new Intent(this,LoginOrSignupActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); return true;

default: return super.onOptionsItemSelected(item);


Q: According the following code, the login credentials cache should be cleared and I should be redirected to the login screen. Unfortunately it keeps on stuck on the same screen.

When I did debugging with my Android Studio 1.2 after startActivity(intent) the IDE jumps to : default: return super.onOptionsItemSelected(item);

Clearly, its a bug and I've tried to use another SDK compiler (SDK 22 instead of 17) and it still does the same.

How this issue can be solved?

Thanks,

Eyalious

4 Answers

Jon Kussmann
PLUS
Jon Kussmann
Courses Plus Student 7,254 Points

Hi Eyal,

Would you mind posting the code for your entire activity? (Also make sure the id of your button in your xml file is "logoutButton" and not something like "logOutButton"... it is case sensitive).

Eyal Cohen
PLUS
Eyal Cohen
Courses Plus Student 438 Points

Hi Jon,

Here is the code of the entire Activity of the Main Feed:

MainFeedActivity.java:

package com.teamtreehouse.parseworkshop;

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

import android.app.ListActivity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.ListView; import android.widget.ProgressBar; import android.widget.SimpleAdapter; import android.widget.TextView;

import com.parse.FindCallback; import com.parse.ParseException; import com.parse.ParseObject; import com.parse.ParseQuery; import com.parse.ParseUser; import com.teamtreehouse.readme.R;

public class MainFeedActivity extends ListActivity {

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

protected ProgressBar mProgressBar;

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

    mProgressBar = (ProgressBar) findViewById(R.id.progressBar1);
}

@Override
public void onResume() {
    super.onResume();
    getLatestPosts();
}

protected void getLatestPosts() {
    mProgressBar.setVisibility(View.VISIBLE);

    /*
     * Use ParseQuery to get latest posts
     */
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    TextView urlLabel = (TextView) v.findViewById(android.R.id.text2);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri.parse(urlLabel.getText().toString()));
    startActivity(intent);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.activity_main_list, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.addButton:
        startActivity(new Intent(this, AddLinkActivity.class));
        return true;
    case R.id.followButton:
        startActivity(new Intent(this, SelectUsersActivity.class));
        return true;
    case R.id.logoutButton:
        /*
         * Log current user out using ParseUser.logOut()
            */
        ParseUser.logOut();

        Intent intent = new Intent(this, LoginOrSignupActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        return true;

    default:
        return super.onOptionsItemSelected(item);
    }
}

}

=================================================================================

activity_main_list.xml:

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/refreshButton"
    android:showAsAction="always"
    android:icon="@drawable/ic_action_refresh"
    android:title="@string/ab_refresh" />

<item
    android:id="@+id/addButton"
    android:showAsAction="always"
    android:title="@string/ab_add" />

<item
    android:id="@+id/followButton"
    android:showAsAction="ifRoom"
    android:title="@string/ab_follow" />

<item
    android:id="@+id/logoutButton"
    android:showAsAction="ifRoom"
    android:title="@string/ab_logout" />

</menu>

=================================================================================

What happens is that on the Main Feed list xml file (activity_main_list.xml) there's an option of Logout. The id is consistent so no case sensitive issue occurs regarding the logout. Moreover, when clicking on logout, the debugger enters into the case of "case R.id.logoutButton" but after "startActivity(intent);" the debugger jumps directly to "default: return super.onOptionsItemSelected(item);"

Instead of "return true;" (Which is on the next row after "startActivity(intent);".

It's clearly a bug but it so subtle.

How it can be fixed? Thanks in Advance,

Eyalious.

Jon Kussmann
PLUS
Jon Kussmann
Courses Plus Student 7,254 Points

Eyalious,

Below is some code from a different project. There are a few differences in how the two methods are implemented. Could you try changing the structure as so?

  @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_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();
        switch(id) {
            case R.id.action_logout:
                ParseUser.logOut();
                navigateToLogin();
                break;
            case R.id.action_edit_friends:
                Intent intent = new Intent(this, EditFriendsActivity.class);
                startActivity(intent);
                break;
            case R.id.action_camera:
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setItems(R.array.camera_choices, mDialogListener);
                AlertDialog dialog = builder.create();
                dialog.show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

For instance, try returning true in onCreateOptionsMenu instead of the super method.

Let me know if this doesn't work... and we'll try something else.

Eyal Cohen
PLUS
Eyal Cohen
Courses Plus Student 438 Points

Hi Jon,

Thank you for your reply.

I can try this out but nevertheless, relating to what I've told, Shouldn't after the "startActivity(intent);" command the "return true" is needed to be executed ( both commands are at the end of the "case R.id.logoutButton:" in the "public boolean onOptionsItemSelected(MenuItem item) " ?

Eyalious

Jon Kussmann
Jon Kussmann
Courses Plus Student 7,254 Points

It does seem odd that it's not working. Have you checked if your LoginOrSignUpActivity is in your AndroidManifest file?