Dominic Sewell
Dominic Sewell

Reputation: 87

Why cant my fragment load the correct view?

I have a fragment that involves the use of two xml files depending on its state. Therefore i wish to set one view "if" a class is active "else" i wish for it to set another view. However i am encountering the error that there is a null object in onCreateView shown in the LOGCAT below

public class MainInstagramActivity extends Fragment {
private InstagramSession mInstagramSession;
private Instagram mInstagram;

private ProgressBar mLoadingPb;
private GridView mGridView;

private static final String CLIENT_ID = "";
private static final String CLIENT_SECRET = "";
private static final String REDIRECT_URI = "";



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

if (mInstagramSession.isActive()) {
    View view = LayoutInflater.from(getActivity()).inflate(
            R.layout.activity_user_instagram, null);
    return view;
}
else {
    View view = LayoutInflater.from(getActivity()).inflate(
            R.layout.activity_main_instagram, null);

    return view;
}
}

@Override
public void onViewCreated(View container, Bundle savedInstanceState) {
super.onViewCreated(container, savedInstanceState);

mInstagram          = new Instagram(getActivity(), CLIENT_ID, CLIENT_SECRET, REDIRECT_URI);

mInstagramSession   = mInstagram.getSession();

if (mInstagramSession.isActive()) {
    View view = LayoutInflater.from(getActivity()).inflate(
            R.layout.activity_user_instagram, null);




    InstagramUser instagramUser = mInstagramSession.getUser();

    mLoadingPb = (ProgressBar)container.findViewById(R.id.pb_loading);
    mGridView   = (GridView)container.findViewById(R.id.gridView);

    ((TextView) container.findViewById(R.id.tv_name)).setText(instagramUser.fullName);
    ((TextView) container.findViewById(R.id.tv_username)).setText(instagramUser.username);

    ((Button) container.findViewById(R.id.btn_logout)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            mInstagramSession.reset();

            startActivity(new Intent(getActivity(), MainInstagramActivity.class));

            getActivity().getFragmentManager().popBackStack();
        }
    });

    ImageView userIv = (ImageView) container.findViewById(R.id.iv_user);

    DisplayImageOptions displayOptions = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.drawable.ic_user)
            .showImageForEmptyUri(R.drawable.ic_user)
            .showImageOnFail(R.drawable.ic_user)
            .cacheInMemory(true)
            .cacheOnDisc(false)
            .considerExifParams(true)
            .build();

    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getActivity())
            .writeDebugLogs()
            .defaultDisplayImageOptions(displayOptions)
            .build();

    ImageLoader imageLoader = ImageLoader.getInstance();
    imageLoader.init(config);

    AnimateFirstDisplayListener animate  = new AnimateFirstDisplayListener();

    imageLoader.displayImage(instagramUser.profilPicture, userIv, animate);

    new DownloadTask().execute();



} else {

    View view = LayoutInflater.from(getActivity()).inflate(
            R.layout.activity_main_instagram, null);

    ((Button) container.findViewById(R.id.btn_connect)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            mInstagram.authorize(mAuthListener);
        }
    });

}
}

LOGCAT

09-22 10:41:31.065      986-986/com.virtualinsomniac.annaakana E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.virtualinsomniac.annaakana, PID: 986
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean         instagramlibrary.InstagramSession.isActive()' on a null object reference
    at com.virtualinsomniac.annaakana.Instagram.MainInstagramActivity.onCreateView(MainInstagramActivity.java:63)
    at android.app.Fragment.performCreateView(Fragment.java:1704)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1063)
    at android.app.BackStackRecord.run(BackStackRecord.java:684)
    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1448)
    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
    at android.os.Handler.handleCallback(Handler.java:738)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5070)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:836)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)

Upvotes: 0

Views: 102

Answers (3)

Ashwin
Ashwin

Reputation: 86

Control goes to onCreateView before onViewCreated. This is the reason you are getting the error. Initialize the instagram and instagramsession objects in onCreateView .. It should work fine !

Put these lines in onCreateView method

mInstagram = new Instagram(getActivity(), CLIENT_ID, CLIENT_SECRET, REDIRECT_URI);

mInstagramSession = mInstagram.getSession();

Upvotes: 2

user468311
user468311

Reputation:

I think that onCreateView is executed before onViewCreated. That's why your reference is null.

From documentation of onViewCreated:

Called immediately after onCreateView(LayoutInflater, ViewGroup, Bundle) has returned

Upvotes: 3

Orest Savchak
Orest Savchak

Reputation: 4569

Your object named mInstagramSession is null. You should first init InstagramSession.

Upvotes: 0

Related Questions