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

LoginActivity.java ~ OnClickListener > signupText EXPLOSION

So I followed Ben's direction to a T and yet I get a NullPointerException.

Arg... Brain Melting.... This can't be so difficult... What am I doing wrong.

    mSignUpTextView = (TextView)findViewById(R.id.signUpText);
    mSignUpTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(LoginActivity.this, SignUpActivity.class);
            startActivity(intent);
        }
    });

Could you post the actual error log you're getting? That code looks fine.

Basically what I have determined is I have two files for a specific process i.e. "activity_sign_up.xml" and "fragment_sign_up.xml". I am only able to build the graphical layout in the fragment xml and when I run the app it crashes with the "NullPointerException." What I have determined is if I copy all of the xml code from the fragment and past it into the "Activity" XML between the <FrameLayout><FrameLayout/> then comment out:

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

Then everything works.

So what I need help with then is how to make the app work with the "fragment.xml" that are generated when I create a new activity.

I don't have an error log for it any longer but I had the same thing happen when I got further along with activity_sign_up.xml and fragment_sign_up.xml so here is the log for that so you can see what the error is like.

LOG: 04-17 13:40:54.996: W/dalvikvm(1102): threadid=1: thread exiting with uncaught exception (group=0xb3a3aba8) 04-17 13:40:55.026: E/AndroidRuntime(1102): FATAL EXCEPTION: main 04-17 13:40:55.026: E/AndroidRuntime(1102): Process: com.appmanimal.izuto_chat, PID: 1102 04-17 13:40:55.026: E/AndroidRuntime(1102): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.appmanimal.izuto_chat/com.appmanimal.izuto_chat.SignUpActivity}: java.lang.NullPointerException 04-17 13:40:55.026: E/AndroidRuntime(1102): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 04-17 13:40:55.026: E/AndroidRuntime(1102): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 04-17 13:40:55.026: E/AndroidRuntime(1102): at android.app.ActivityThread.access$800(ActivityThread.java:135) 04-17 13:40:55.026: E/AndroidRuntime(1102): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 04-17 13:40:55.026: E/AndroidRuntime(1102): at android.os.Handler.dispatchMessage(Handler.java:102) 04-17 13:40:55.026: E/AndroidRuntime(1102): at android.os.Looper.loop(Looper.java:136) 04-17 13:40:55.026: E/AndroidRuntime(1102): at android.app.ActivityThread.main(ActivityThread.java:5017) 04-17 13:40:55.026: E/AndroidRuntime(1102): at java.lang.reflect.Method.invokeNative(Native Method) 04-17 13:40:55.026: E/AndroidRuntime(1102): at java.lang.reflect.Method.invoke(Method.java:515) 04-17 13:40:55.026: E/AndroidRuntime(1102): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 04-17 13:40:55.026: E/AndroidRuntime(1102): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 04-17 13:40:55.026: E/AndroidRuntime(1102): at dalvik.system.NativeStart.main(Native Method) 04-17 13:40:55.026: E/AndroidRuntime(1102): Caused by: java.lang.NullPointerException 04-17 13:40:55.026: E/AndroidRuntime(1102): at com.appmanimal.izuto_chat.SignUpActivity.onCreate(SignUpActivity.java:31)

Ok, that says this error is from SignUpActivity line 31, so could you post that region of code?

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.signup_error_message)
                    .setTitle(R.string.signup_error_title)
                    .setPositiveButton(android.R.string.ok, null);
                AlertDialog dialog = builder.create();
                dialog.show();
            }
            else {
                // Create a new user
            }

        }

There aren't any numbers in the code you just pasted, so I'm not sure where line 31 is, but this is the only possible issue I can think of.

The layout that you are using in your onCreate is not the right one. You may accidentally have put the layout information (TextViews, Buttons, etc) in fragment_signup.xml instead of activity_signup.xml. Therefore when the activity is loaded and your code starts looking for id's in the xml that aren't there, any information you try to pull from non-existent fields will throw a null pointer exception. -- I'm referring to mEmail, mUsername, and mPassword

like -- mUsername = (EditText) findViewById(R.id.usernameField); // this might work here, but when you try //String username = mUsername.getText().toString(); // you get a null pointer exception because youre pulling from the wrong (non-existent) field

hopefully that helps.

Thanks for trying to help Marco. Line 31 is mSignUpButton.setOnClickListener(new View.OnClickListener() {

I fixed the problem with a work around by coping all of the xml code from the "fragment" and pasted it into the "Activity" XML between the "FrameLayout" flags and then in the java code I commented out:

// File Name: LoginActivity.java @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_login, container, false); return rootView; }

// File Name: SignUpActivity.java if (savedInstanceState == null) { getFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment()).commit(); }

public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

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

1 Answer

*put this in an answer instead of comment by mistake, whoops. Will edit when I have the answer. *