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

Stuck on "Error Messages with Dialogs" video in the ribbit app. Please help!

Hello,

I am building the ribbit app and I am on the "Error Messages with Dialogs" video. My code is not showing any errors and when I run the app it starts. However, when I click on the "Sign Up" label to go to the Sign Up Activity to text my error message dialog the app crashes. The error on the emulator I get is "Unfortunately, Ribbit has stopped."

in LogCat I see the following errors:

06-04 04:14:18.578: E/AndroidRuntime(1469): FATAL EXCEPTION: main 06-04 04:14:18.578: E/AndroidRuntime(1469): Process: com.erinkabbash.ribbit, PID: 1469 06-04 04:14:18.578: E/AndroidRuntime(1469): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.erinkabbash.ribbit/com.erinkabbash.ribbit.SignUpActivity}: java.lang.NullPointerException 06-04 04:14:18.578: E/AndroidRuntime(1469): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 06-04 04:14:18.578: E/AndroidRuntime(1469): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 06-04 04:14:18.578: E/AndroidRuntime(1469): at android.app.ActivityThread.access$800(ActivityThread.java:135) 06-04 04:14:18.578: E/AndroidRuntime(1469): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 06-04 04:14:18.578: E/AndroidRuntime(1469): at android.os.Handler.dispatchMessage(Handler.java:102) 06-04 04:14:18.578: E/AndroidRuntime(1469): at android.os.Looper.loop(Looper.java:136) 06-04 04:14:18.578: E/AndroidRuntime(1469): at android.app.ActivityThread.main(ActivityThread.java:5017) 06-04 04:14:18.578: E/AndroidRuntime(1469): at java.lang.reflect.Method.invokeNative(Native Method) 06-04 04:14:18.578: E/AndroidRuntime(1469): at java.lang.reflect.Method.invoke(Method.java:515) 06-04 04:14:18.578: E/AndroidRuntime(1469): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 06-04 04:14:18.578: E/AndroidRuntime(1469): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 06-04 04:14:18.578: E/AndroidRuntime(1469): at dalvik.system.NativeStart.main(Native Method) 06-04 04:14:18.578: E/AndroidRuntime(1469): Caused by: java.lang.NullPointerException 06-04 04:14:18.578: E/AndroidRuntime(1469): at com.erinkabbash.ribbit.SignUpActivity.onCreate(SignUpActivity.java:28) 06-04 04:14:18.578: E/AndroidRuntime(1469): at android.app.Activity.performCreate(Activity.java:5231) 06-04 04:14:18.578: E/AndroidRuntime(1469): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 06-04 04:14:18.578: E/AndroidRuntime(1469): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 06-04 04:14:18.578: E/AndroidRuntime(1469): ... 11 more

The code in my SignUpActivity.java is:

package com.erinkabbash.ribbit;

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

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);
        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 the new user
                }                   
            }
        });

    }

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

Any ideas what I am missing? I cannot seem to figure out what is crashing the app.

Thanks in advanced for any help you can provide!

Sincerely,

Erin S Kabbash

2 Answers

I figured it out myself.. I changed setContentView(R.layout.activity_sign_up); to setContentView(R.layout.fragment_sign_up);

It appears the I am using fragments somehow and the tutorial is not. I have no experience with fragments yet so unsure if I should move all of my layout code into activity_sign_up or if I can just keep it in the fragment_sign_up and just reference that layout instead. I am assuming it doesn't really matter for now as the app is functioning properly. If this is not the case and someone reading this is aware please let me know! Otherwise, this issue has been resolved by myself. Yay!

Hi Erin,

Unfortunately the course was written only a few months before the ADT (Android Development Tools) plugin for Eclipse was updated to automatically include fragments in new Android projects. In the long run this will probably have been a good choice given the added UI functionality they provide (you can read about them here).

However, this didn't prevent the move from being massively confusing for many people who have either never heard of or used fragments.

You have two options - either you can delete all of the Fragment references in your code and move the content from the Fragment XML files to their normal counterparts (ie. replacing everything in activity_main with fragment_main), or you can continue to use fragments in your project. The difference will only matter if you explicitly want to use fragments for a particular feature in your application.