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 Build a Self-Destructing Message Android App Adding Users Using Parse.com Logging In

Ricky Sparks
Ricky Sparks
22,249 Points

Android Ribbit App- signed up first user to parse and then blank screen on emulator happened?

Here are screenshots of my logcat and emulator

http://i.imgur.com/Hd6yThg.png?1

http://i.imgur.com/VugzaEs.png?1

6 Answers

OK - I'm not sure where you're at with the development of this app ... stick at it, though!

You're having difficulty when you create a new user, right? So the issue might sit inside SignUpActivity.java?

In my version of that, after we've checked that the username, password and email are not empty, we go on to create a new user within Parse. This is done with a signUpInBackground method after creating a new Parse user object with the three variables above.

The code after this is key - in my version (which may have progressed from the part of the course you have reached) it creates a new Intent, adds some flags from the ParseConstants java file (I think you've called that ParseObject (which may cause a problem), then starts the intent with startActivity(intent).

Does that sound similar to your code? Can you post your code with a copy & paste so we can look at it?

Cheers,

Steve.

Ricky Sparks
Ricky Sparks
22,249 Points

package com.patriothighschool.rickyjames1750.ribbit;

import android.app.Activity; import android.app.AlertDialog; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.Window; import android.widget.Button; import android.widget.EditText;

import com.parse.ParseException; import com.parse.ParseUser; import com.parse.SignUpCallback;

public class SignUpActivity extends Activity {

protected EditText mUsername;
protected EditText mPassword;
protected EditText mEmail;
protected Button mSignUpButton;

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

    mUsername = (EditText)findViewById(R.id.usernameField);
    mPassword = (EditText)findViewById(R.id.passwordField);
    mEmail= (EditText)findViewById(R.id.emailField);
    mSignUpButton = (Button)findViewById(R.id.signupButton);
    mSignUpButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String username = mUsername.getText().toString();
            String password = mPassword.getText().toString();
            String email = mEmail.getText().toString();

            username = username.trim();
            password = password.trim();
            email = email.trim();

            if (username.isEmpty() || password.isEmpty() || email.isEmpty()) {
                AlertDialog.Builder builder = new AlertDialog.Builder(SignUpActivity.this);
                builder.setMessage(R.string.sign_up_error_message)
                        .setTitle(R.string.sign_up_error_title)
                        .setPositiveButton(android.R.string.ok, null);
                AlertDialog dialog = builder.create();
                dialog.show();
            } else {
                //create a new user!
                setProgressBarIndeterminate(true);
                ParseUser newUser = new ParseUser();
                newUser.setUsername(username);
                newUser.setPassword(password);
                newUser.setEmail(email);
                newUser.signUpInBackground(new SignUpCallback() {
                    @Override
                    public void done(ParseException e) {
                        setProgressBarIndeterminate(false);
                        if (e == null) {
                            //success
                            Intent intent = new Intent(SignUpActivity.this, MainActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(intent);
                        }
                        else {
                            AlertDialog.Builder builder = new AlertDialog.Builder(SignUpActivity.this);
                            builder.setMessage(e.getMessage())
                                    .setTitle(R.string.sign_up_error_title)
                                    .setPositiveButton(android.R.string.ok, null);
                            AlertDialog dialog = builder.create();
                            dialog.show();
                        }
                    }
                });
            }
        }
    });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.sign_up, 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();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

Can you see the added user within Parse.com?

If you post your FriendsFragment.java code in here, that might help us too. I think that's where the user list is fetched.

Steve.

Ok. Try adding more Parse users but also post your code then we can spot the issue, perhaps.

Ricky Sparks
Ricky Sparks
22,249 Points

My Java folder doesn't have FriendsFragments but here is my event log don't really get what it means?

http://i.imgur.com/Ii623Y2.png?1

Ricky Sparks
Ricky Sparks
22,249 Points

My emulator is blank and wont let me add any users either?

http://i.imgur.com/LNsiiWn.png?1

I'll have a look at my code on the Mac when I get home and see what we need to see. There'll be a background function accessing Parse that should bring back the users. I'll post later.

I can't see anything wrong with that code. I'll have a walk through the process on my version to see where else the problem may lie.

This could be an issue within Parse. Do your heading names match and is the application in RibbittApplication using the correct MD5 (or whatever they are) codes?

My Parse Core looks like:

Parse core screenshot

Yours may differ but the column names across the top must match what is in your application exactly. Mabe post your Parse Core stuff for Ribbit.

Next - I guess we should start with what you're expecting to see. I've not been at that stage of the course for a long time so can't remember precisely how it wored as I worked through the exercises.

In mine, when I sign up as a new user, I get taken to the Inbox which, obviously, is blank as I've got no messages yet. However, the inbox is loaded from the InboxFragment which you haven't got yet. I can't remember where it was originally located - have you built a List somewhere to cope with messages and/or friends?

protected List<ParseObject> mMessages

Where is that code in your file structure?

If the Parse piece is working, i.e. you can 'Sign Up!' as many users as you want, then the bit that is failing is probably the part of code that should be fired when the application starts up. From a quick look, MainActivity checks if a user is logged in. If one isn't then it displays the Login screen. From there, you selected "Sign UP!" instead which allows you to create a new account. Once this is completed, the Intent takes you back to MainActivity where, this time, there is a signed in user, i.e. the else part of the if (currentUser == null){ conditional test is fired.

This should write a Log.i to your LogCat - does it? It should show the logged in user's name.

Next, the action bar is created. This seems to be the part that isn't working for you. So, let's have a look at all your code in MainActivity - maybe we can find the problem in there!

We'll get there in the end!

Steve.

Ricky Sparks
Ricky Sparks
22,249 Points

This is what my MainActivity code is but there are no red marks?

package com.patriothighschool.rickyjames1750.ribbit;

import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.content.Intent; import android.os.Bundle; import android.support.v13.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup;

import com.parse.ParseAnalytics; import com.parse.ParseUser;

import java.util.Locale;

public class MainActivity extends Activity { public static final String TAG = MainActivity.class.getSimpleName();

/**
 * The {@link android.support.v4.view.PagerAdapter} that will provide
 * fragments for each of the sections. We use a
 * {@link 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.v13.app.FragmentStatePagerAdapter}.
 */
SectionsPagerAdapter mSectionsPagerAdapter;

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

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



    ParseAnalytics.trackAppOpened(getIntent());

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



    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());

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

    ParseObject object;

}

private void navigateToLogin() {
    Intent intent = new Intent(this, LoginActivity.class);
    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) {
    // 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();
    if (id == R.id.action_logout) {
        ParseUser.logOut();
        navigateToLogin();

    }
    return super.onOptionsItemSelected(item);
}



/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        return PlaceholderFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }

    //@Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
            case 0:
                return getString(R.string.title_section1).toUpperCase(l);
            case 1:
                return getString(R.string.title_section2).toUpperCase(l);
            case 2:
                return getString(R.string.title_section3).toUpperCase(l);
        }
        return null;
    }
}


/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;
    }
}

}

Apologies for the delay here - I've moved house and the place is chaos! And my broadband still doesn't work so I've hookied everything up over a 3G tablet as a wireless hotspot. SLOW is not the word!

Anyway, I can't see much wrong with this lot, to be honest, but there are a few differences. These may be because I've continued with the course so the code has progressed.

In SignUpActivity you make two changes to the Progress Bar. One says setProgressBarIndeterminate(true) and the other sets it to (false). My code is slightly different - it uses setProgressBarIndeterminateVisibility(true) - the same for the false setting too. Give that a try, perhaps! I can't see that fixing much, though.

There's a complete lack of any ActionBar code in your MainActivity, I'll assume that this comes later in the course. However, another bit of code that is slightly different is where you have:

mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());

I have:

mSectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());

Again, I don't know if that'll help, or not.

It might be worth making sure your Intent navigation works without the Parse bits in there although, as they operate in the background, that should make no difference.

What do you expect to see when you've signup up a new user? My app navigates to the Inbox which is a fragment - you'venot reached that bit yet.

If none of the above works, post a zip of all your project files and I'll work through the course to the same point again and see where the differences are.

Steve.

Ricky Sparks
Ricky Sparks
22,249 Points

Had the same problem when i was using my windows phone 3G hotspot and I think what you said helped because there's a possibility my main screen tabs doesn't even exist in code. Must have chosen the wrong template when this app was first created. My activity was blank i guess not frozen.

Hope you got it sorted.

Steve.