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

Daniel Quaidoo
Daniel Quaidoo
23,937 Points

Navigation Drawer crashing

I have a navigation drawer and I'm using Android Studio and the NavigationDrawerFragment. Been able to setup my Navigation drawer to call other fragments into view. However now I'm unable to call an intent within a fragment

``import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView;

// A placeholder fragment containing a simple view.

public class LoginActivity extends Fragment {

 //The fragment argument representing the section number for this
 //fragment.

private static final String ARG_SECTION_NUMBER = "section_number";
public static final String TAG = LoginActivity.class.getSimpleName();
Context mContext;
protected TextView mSignUpTextView;


 // Returns a new instance of this fragment for the given section
 //number.

public static LoginActivity newInstance(int sectionNumber) {
    LoginActivity fragment = new LoginActivity();
    Bundle args = new Bundle();
    args.putInt(ARG_SECTION_NUMBER, sectionNumber);
    fragment.setArguments(args);
    return fragment;
}

public LoginActivity() {

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

        }

    });
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View rootView = inflater.inflate(R.layout.activity_login, container, false);
    mContext = rootView.getContext();

    return rootView;
}

}``

There is my fragment code. Anything I might be doing wrong?

1 Answer

Alessandro Calorì
Alessandro Calorì
10,241 Points

You are calling getActivity() from the constructor of the Fragment. At that stage the fragment is not attached to your activity. You should call it in an overridden method such as onActivityCreated or onAttach.