Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android

Daniel Quaidoo
Daniel Quaidoo
23,410 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.