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

Android studio deprecated functions

I am running Android Studio 1.2 and many functions and methods are deprecated in onCreate Method i.e., setNavigationMode, Navigation_mode_tabs, setSelectedNavigationItem,addTab, newTab, ActionBarActivity, and TabListener.

What replacements can we use. I am on the Build a Self Destructing Message App and stuck because of this. Once I added the Logout code to my app it began to crash.

Thank You.

15 Answers

Nicolas Hampton
Nicolas Hampton
44,725 Points

I have a whole explanation and fix with undeprecated code here:

https://teamtreehouse.com/forum/this-is-the-droid-you-are-looking-for-how-to-get-past-this-lesson

...also, make sure your imports for the Fragment, fragment manager, and FragmentPagerAdapter are all the version 4 support library imports.

Thanks, I will check this out.

Nicolas Hampton
Nicolas Hampton
44,725 Points

Let me know if it's clear or if there's anything you need an explanation on or if you run across a conflict later on, this has been a big problem for the class, so feedback would be a help for everybody.

Will do. So far this is the best information provided on this issue.

I added the code to the activty_main.xml file, then I added to the MainActivity file, but I keep throwing a few errors. First, my ActionBarActivity is deprecated so how do I get around that? Also, my errors are:
SectionsPagerAdapter mSectionsPagerAdapter; **error on SectionsPagerAdapter

mSectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager()); **error on SectionsPagerAdapter

mViewPager.setAdapter(mSectionsPagerAdapter); **This error says, setAdapter (android.support.v4.view.PagerAdapter) in ViewPager cannot be applied to (SectionsPagerAdapter)

public class MainActivity extends ActionBarActivity {

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.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);
    setContentView(R.layout.activity_main);

    // Get the user cached on disk if present
    ParseUser currentUser = ParseUser.getCurrentUser();

    if (currentUser == null) {
        // If no one is logged in, go to the login screen
        navigateToLogin();
    } else {
        Log.i(TAG, currentUser.getUsername());
    }


    // Set up the action bar.
    final ActionBar actionBar = getSupportActionBar();

    // ***This is where setNavigationMode has been deprecated***


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

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);
}
Nicolas Hampton
Nicolas Hampton
44,725 Points

All my action bar imports are support version 7. All my fragment imports are support version 4. Make sure you are only using these versions of the imports and that there are no other versions imported. This would cause the most obvious error I can think of. From what I can tell, in my SectionsPagerAdapter I've imported:

import java.util.Locale;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

and in my main activity, the imports used are

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import com.parse.ParseUser;

these are the only imports being used in my program, so you can cut and paste and the code should run if this is the problem. I've tried it and it runs with only these imports. Let me know.

Nicolas Hampton
Nicolas Hampton
44,725 Points

Also, please when placing code into questions, comments and answers, make sure that you use the correct break to property display the code. Give one line of space after your previous text, then three hash marks (the symbol next to the number one) followed by the language (java), then another return space and then the code. After the code, do three more hash marks to close the box. This displays the code with all proper highlighting and makes it 100 times easier to read for people that want to help, the notes for this notation are in the link "Markdown Cheatsheet" at the bottom of each comment box. It helps me a lot!

Will do.

I'm not showing any errors, but the app still won't run when I start it. Any suggestions? Thanks.

Nicolas Hampton
Nicolas Hampton
44,725 Points

What do you mean? Are you using the genymotion emulator? What version is the virtual machine you're using? Does the app give errors not in editor, but at runtime? What does your code look like? Is your version of Android studio up to date? Request more info.

I've been using my android device to run the apps. And I'm not seeing any errors at all. I'm using Android Studio 1.2, didn't have any issues until I updated with this version. This is my code from MainActivity and Activity_Main.xml

package lejacque1.ribbit2;
import java.util.Locale;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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 android.support.v4.app.FragmentActivity;
import com.parse.ParseUser;
import android.support.v4.app.ListFragment;

    // ***The TabListener has been deprecated, will look for update later***
    // ***The MainActivity class is implementing TabListener, so the     ***
    // *** activity itself is listening for tab changes. We'll have to   ***
    // *** replace this later. Remember that TabListener is an interface,***
    // *** so we'll be looking for a replacement interface later.        ***
    public class MainActivity extends ActionBarActivity {

    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.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);
        setContentView(R.layout.activity_main);

        // Get the user cached on disk if present
        ParseUser currentUser = ParseUser.getCurrentUser();

        if (currentUser == null) {
            // If no one is logged in, go to the login screen
            navigateToLogin();
        } else {
            Log.i(TAG, currentUser.getUsername());
        }


        // Set up the action bar.
        final ActionBar actionBar = getSupportActionBar();

        // ***This is where setNavigationMode has been deprecated***


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

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



    @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();

        //noinspection SimplifiableIfStatement
        switch (id) {

            case R.id.action_logout:
                ParseUser.logOut();
                navigateToLogin();
                break;


        }
        return super.onOptionsItemSelected(item);
    }


    private void navigateToLogin() {
        Intent intent = new Intent(MainActivity.this, LoginActivity2Activity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }
    /**
     * A placeholder fragment containing a simple view.
     * This is referred to as "DummySectionFragment" in the
     * lesson.
     */
    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";

        public PlaceholderFragment() {
        }

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

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

}


<android.support.v4.view.ViewPager android:id="@+id/pager"
                                   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=".MainActivity">

    // *** Here, we add our child view PagerTabStrip***
    <android.support.v4.view.PagerTabStrip
        android:id="@+id/pager_title_strip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="#33b5e5"
        android:textColor="#fff"
        android:paddingTop="4dp"
        android:paddingBottom="4dp" />


</android.support.v4.view.ViewPager>

These are the errors I got in Logcat: Thanks for your help so far and I understand if it's alot to go through.

05-11 11:50:51.743  18235-18235/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.UnsupportedOperationException: Required method instantiateItem was not overridden
            at android.support.v4.view.PagerAdapter.instantiateItem(PagerAdapter.java:175)
            at android.support.v4.view.PagerAdapter.instantiateItem(PagerAdapter.java:110)
            at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:836)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:986)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:918)
            at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1440)
            at android.view.View.measure(View.java:15293)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
            at android.view.View.measure(View.java:15293)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
            at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:444)
            at android.view.View.measure(View.java:15293)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.view.View.measure(View.java:15293)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1418)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:709)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:602)
            at android.view.View.measure(View.java:15293)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2425)
            at android.view.View.measure(View.java:15293)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1876)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1112)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1301)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1010)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4255)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
            at android.view.Choreographer.doCallbacks(Choreographer.java:555)
            at android.view.Choreographer.doFrame(Choreographer.java:525)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4849)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
            at dalvik.system.NativeStart.main(Native Method)

E/Trace﹕ error opening trace file: No such file or directory (2)

E/InputDispatcher﹕ channel '427fea08 lejacque1.ribbit2/lejacque1.ribbit2.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!

Could not find class 'android.app.Notification$Action$Builder', referenced from method b.a

Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method b.a

E/NetworkScheduler.SchedulerReceiver﹕ Invalid parameter app 05-11 11:39:03.868 669-669/? E/NetworkScheduler.SchedulerReceiver﹕ Invalid package name : Perhaps you didn't include a PendingIntent in the extras? 05-11 11:39:03.892 6068-17171/? D/PackageBroadcastService﹕ Received broadcast action=android.intent.action.PACKAGE_REMOVED and uri=lejacque1.ribbit2 05-11 11:39:03.923 17129-17174/? E/dalvikvm﹕ Could not find class 'android.os.UserManager', referenced from method f.<init>

Nicolas Hampton
Nicolas Hampton
44,725 Points

The file activity_main.xml shouldn't have any capitals in it. Also, later we're going to take out the entire PlaceholderFragment nested class, so we don't need it, but leave it for now. I think the problem is coming from your adapter. Can we see the code for that?

I'm not sure about the adapter, but here is the sode from my signin and login classes

package lejacque1.ribbit2;

import android.app.AlertDialog;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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 SignUpActivity2Activity extends AppCompatActivity {

    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_activity2);

        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 view) {
                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(SignUpActivity2Activity.this);
                    builder.setMessage(R.string.signup_error_message)
                            .setTitle(R.string.signup_error_title)
                            .setPositiveButton(android.R.string.ok, null);

                    AlertDialog dialog = builder.create();
                    dialog.show();
                }
                else {
                    setProgressBarIndeterminateVisibility(true);

                    ParseUser newUser = new ParseUser();
                    newUser.setUsername(username);
                    newUser.setPassword(password);
                    newUser.setEmail(email);
                    newUser.signUpInBackground(new SignUpCallback() {
                        @Override
                        public void done(ParseException e) {
                            setProgressBarIndeterminateVisibility(false);

                            if (e == null) {
                                //success
                                Intent intent = new Intent(SignUpActivity2Activity.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(SignUpActivity2Activity.this);
                                builder.setMessage(e.getMessage())
                                        .setTitle(R.string.signup_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.menu_sign_up_activity2, 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);
    }
}
package lejacque1.ribbit2;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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 android.widget.TextView;

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


public class LoginActivity2Activity extends AppCompatActivity {

    protected EditText mUsername;
    protected EditText mPassword;
    protected Button mLoginButton;
    protected TextView mSignUpTextView;

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

        mSignUpTextView = (TextView) findViewById(R.id.signUpText);
        mSignUpTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity2Activity.this,
                        SignUpActivity2Activity.class);
                startActivity(intent);

            }
        });

        mUsername = (EditText) findViewById(R.id.usernameField);
        mPassword = (EditText) findViewById(R.id.passwordField);
        mLoginButton = (Button) findViewById(R.id.loginButton);
        mLoginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username = mUsername.getText().toString();
                String password = mPassword.getText().toString();

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

                if (username.isEmpty() || password.isEmpty()) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity2Activity.this);
                    builder.setMessage(R.string.login_error_message)
                            .setTitle(R.string.login_error_title)
                            .setPositiveButton(android.R.string.ok, null);

                    AlertDialog dialog = builder.create();
                    dialog.show();
                } else {

                    setProgressBarIndeterminateVisibility(true);

                    ParseUser.logInInBackground(username, password, new LogInCallback() {
                        @Override
                        public void done(ParseUser user, ParseException e) {
                            setProgressBarIndeterminateVisibility(false);

                            if (e == null) {
                                Intent intent = new Intent(LoginActivity2Activity.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(LoginActivity2Activity.this);
                                builder.setMessage(e.getMessage())
                                        .setTitle(R.string.login_error_title)
                                        .setPositiveButton(android.R.string.ok, null);

                                AlertDialog dialog = builder.create();
                                dialog.show();
                            }
                        }
                    });
                }
            }
        });
    }


                    @Override //ADDED, ORIGINAL CODE IS BELOW.
                     public boolean onCreateOptionsMenu(Menu menu) {
                     // Inflate the menu; this adds items to the action bar if it is present.
                     getMenuInflater().inflate(R.menu.menu_login_activity2, 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);
                    }
                }*/
package lejacque1.ribbit2;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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 android.widget.TextView;

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


public class LoginActivity2Activity extends AppCompatActivity {

    protected EditText mUsername;
    protected EditText mPassword;
    protected Button mLoginButton;
    protected TextView mSignUpTextView;

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

        mSignUpTextView = (TextView) findViewById(R.id.signUpText);
        mSignUpTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity2Activity.this,
                        SignUpActivity2Activity.class);
                startActivity(intent);

            }
        });

        mUsername = (EditText) findViewById(R.id.usernameField);
        mPassword = (EditText) findViewById(R.id.passwordField);
        mLoginButton = (Button) findViewById(R.id.loginButton);
        mLoginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username = mUsername.getText().toString();
                String password = mPassword.getText().toString();

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

                if (username.isEmpty() || password.isEmpty()) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity2Activity.this);
                    builder.setMessage(R.string.login_error_message)
                            .setTitle(R.string.login_error_title)
                            .setPositiveButton(android.R.string.ok, null);

                    AlertDialog dialog = builder.create();
                    dialog.show();
                } else {

                    setProgressBarIndeterminateVisibility(true);

                    ParseUser.logInInBackground(username, password, new LogInCallback() {
                        @Override
                        public void done(ParseUser user, ParseException e) {
                            setProgressBarIndeterminateVisibility(false);

                            if (e == null) {
                                Intent intent = new Intent(LoginActivity2Activity.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(LoginActivity2Activity.this);
                                builder.setMessage(e.getMessage())
                                        .setTitle(R.string.login_error_title)
                                        .setPositiveButton(android.R.string.ok, null);

                                AlertDialog dialog = builder.create();
                                dialog.show();
                            }
                        }
                    });
                }
            }
        });
    }


                    @Override //ADDED, ORIGINAL CODE IS BELOW.
                     public boolean onCreateOptionsMenu(Menu menu) {
                     // Inflate the menu; this adds items to the action bar if it is present.
                     getMenuInflater().inflate(R.menu.menu_login_activity2, 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);
                    }
                }*/
package lejacque1.ribbit2;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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 android.widget.TextView;

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


public class LoginActivity2Activity extends AppCompatActivity {

    protected EditText mUsername;
    protected EditText mPassword;
    protected Button mLoginButton;
    protected TextView mSignUpTextView;

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

        mSignUpTextView = (TextView) findViewById(R.id.signUpText);
        mSignUpTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity2Activity.this,
                        SignUpActivity2Activity.class);
                startActivity(intent);

            }
        });

        mUsername = (EditText) findViewById(R.id.usernameField);
        mPassword = (EditText) findViewById(R.id.passwordField);
        mLoginButton = (Button) findViewById(R.id.loginButton);
        mLoginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username = mUsername.getText().toString();
                String password = mPassword.getText().toString();

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

                if (username.isEmpty() || password.isEmpty()) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity2Activity.this);
                    builder.setMessage(R.string.login_error_message)
                            .setTitle(R.string.login_error_title)
                            .setPositiveButton(android.R.string.ok, null);

                    AlertDialog dialog = builder.create();
                    dialog.show();
                } else {

                    setProgressBarIndeterminateVisibility(true);

                    ParseUser.logInInBackground(username, password, new LogInCallback() {
                        @Override
                        public void done(ParseUser user, ParseException e) {
                            setProgressBarIndeterminateVisibility(false);

                            if (e == null) {
                                Intent intent = new Intent(LoginActivity2Activity.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(LoginActivity2Activity.this);
                                builder.setMessage(e.getMessage())
                                        .setTitle(R.string.login_error_title)
                                        .setPositiveButton(android.R.string.ok, null);

                                AlertDialog dialog = builder.create();
                                dialog.show();
                            }
                        }
                    });
                }
            }
        });
    }


                    @Override //ADDED, ORIGINAL CODE IS BELOW.
                     public boolean onCreateOptionsMenu(Menu menu) {
                     // Inflate the menu; this adds items to the action bar if it is present.
                     getMenuInflater().inflate(R.menu.menu_login_activity2, 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);
                    }
                }*/

This is my SectionsPagersAdapter

package lejacque1.ribbit2;

import android.support.v4.app.FragmentManager;
import android.support.v4.view.PagerAdapter;
import android.view.View;

/**
 * Created by yuriana on 10/05/2015.
 */
public class SectionsPagerAdapter extends PagerAdapter {
    public SectionsPagerAdapter(MainActivity mainActivity, FragmentManager supportFragmentManager) {
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return false;
    }
}
Nicolas Hampton
Nicolas Hampton
44,725 Points

There's your problem. It should be this:

package com.staggarlee.ribbit;

/**
 * Created by nicolas on 5/9/15.
 */

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.Locale;

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

    // In the lesson, we factor this code out of MainActivity.class, and thus must add the context
    // as a member variable that gets passed in through the constructor. Nothing about this
    // will change from the video, no deprecation involved, no Android Studio conflicts.

    protected Context mContext;

    public SectionsPagerAdapter(Context context, FragmentManager fm) {
        super(fm);
        mContext = context;
    }

    @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 MainActivity.PlaceholderFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        // In the video we reduce this boilerplate value from 3 
        // to 2 tabs, one for Inbox and one for Friends. Again, no 
        // change needed from the video, follow along.
        // Show 2 total pages.
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        // this is where we'll pass the context to variables that are now
        // outside the scope of MainActivity.class after the refactor.
        // again, nothing different than the video, follow the video.
        // Remember to delete the third case in the boilerplate code
        // and change the strings resources entries to Inbox and 
        // Friends, respectively, just like in the video.
        switch (position) {
            case 0:
                return mContext.getString(R.string.title_section1).toUpperCase(l);
            case 1:
                return mContext.getString(R.string.title_section2).toUpperCase(l);
        }
        return null;
    }
}

Later, PlaceholderFragment will be replaced by InboxFragment and FriendsFragment and we will have to change this getItem function to reflect that, but that's a later lesson. This should fix your issues.

I replaced my old one but it's still not loading. Keeps saying "Unfortunately, Ribbit has stopped." Thanks for all the help you've given me thus far. Maybe I need to redo the program from scratch or just scrap it all together.

Nicolas Hampton
Nicolas Hampton
44,725 Points

Is there a list of runtime errors in the debugger after it crashes? Lets work with this a bit. Working through these errors is what programming's all about, I was doing the same thing making these instructions, so don't feel bad. Do you have the error messages?

just this in the console. The debugger says "frame now available."

Waiting for device. Target device: lge-lgms769-1570000600000001016385A31701B01? Uploading file local path: C:\Users\yuriana\AndroidStudioProjects\Ribbit2\app\build\outputs\apk\app-debug.apk remote path: /data/local/tmp/lejacque1.ribbit2 No apk changes detected. Skipping file upload, force stopping package instead. DEVICE SHELL COMMAND: am force-stop lejacque1.ribbit2 Launching application: lejacque1.ribbit2/lejacque1.ribbit2.MainActivity. DEVICE SHELL COMMAND: am start -D -n "lejacque1.ribbit2/lejacque1.ribbit2.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=lejacque1.ribbit2/.MainActivity }

Warning: debug info can be unavailable. Please close other application using ADB: Monitor, DDMS, Eclipse Waiting for process: lejacque1.ribbit2 Connected to the target VM, address: 'localhost:8601', transport: 'socket' Disconnected from the target VM, address: 'localhost:8601', transport: 'socket'

Nicolas Hampton
Nicolas Hampton
44,725 Points

I think you've used eclipse before android studio and now both programs are requesting the same device for some reason. See if you can't shut down eclipse, or if it's not on, reset the computer and try again after only opening android studio. It looks like they're trying to access the same port at the same time.

I started this on eclipse then moved over to android. That was a week or so ago. The other tests on the program went through fine, but when I added the last part with the logout feature I started getting these issues. Should I uninstall eclipse since it's out of date now anyway.

I got this in my custom_action_bar.xml file: Valid XML document must have a root tag. It's just showing a red squigly line under the < of the bottom </LinearLayout>.

My AndroidManifest file

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


    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:name=".Ribbit2Application" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".LoginActivity2Activity"
            android:label="@string/title_activity_login_activity2"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name=".SignUpActivity2Activity"
            android:label="@string/title_activity_sign_up_activity2"
            android:screenOrientation="portrait" >

        </activity>
    </application>

</manifest>
Nicolas Hampton
Nicolas Hampton
44,725 Points

I don't see the edit friends activity in that manifest?

We didn-t get to that part yet, the next thing after adding the logout, which is where i started crashing, was adding a spinner.