-4

Here's the fragment class code which has a logout button.

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.marshall.gruppo.R;
import com.marshall.gruppo.helper.SQLiteHandler;
import com.marshall.gruppo.helper.SessionManager;

public class MyPageFragment extends Fragment implements View.OnClickListener {

    Button logout;
    View view;
    SessionManager session;
    SQLiteHandler db;

    public MyPageFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_mypage, container, false);
        logout = (Button) view.findViewById(R.id.logout);
        logout.setOnClickListener(this);
        return rootView;
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.logout:
                logoutUser();
                break;
        }
    }
    private void logoutUser() {
        session.setLogin(false);
        db.deleteUsers();
        Intent intent = new Intent(getActivity().getApplicationContext(), LoginActivity.class);
        startActivity(intent);
        getActivity().finish();
    }
}

But when I run the code, it throws a NullPointerException with the following log.

07-10 13:03:25.521  13363-13363/com.marshall.gruppo E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.marshall.gruppo, PID: 13363
    java.lang.NullPointerException
            at com.marshall.gruppo.ui.MyPageFragment.onCreateView(MyPageFragment.java:28)
            at android.app.Fragment.performCreateView(Fragment.java:1700)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
            at android.app.BackStackRecord.run(BackStackRecord.java:684)
            at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1453)
            at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5602)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)

When writing the code it did not show any errors, so I really wonder where it has a fault.. According to the log, it is this part that throws the exception.

logout = (Button) view.findViewById(R.id.logout);

Any advice on this issue?

Naman Gala
  • 4,670
  • 1
  • 21
  • 55
MarshallLee
  • 1,290
  • 4
  • 23
  • 42

2 Answers2

1

In your code you are using view.findViewById. here view is null. That's why you are getting NullPointerException.

Instead of this code:

View rootView = inflater.inflate(R.layout.fragment_mypage, container, false);

    logout = (Button) view.findViewById(R.id.logout);

Try this code:

View rootView = inflater.inflate(R.layout.fragment_mypage, container, false);
        logout = (Button) rootView.findViewById(R.id.logout);
Anand Singh
  • 5,672
  • 2
  • 23
  • 33
0

Try it like this

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

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onViewCreated(view, savedInstanceState);
     logout = (Button) view.findViewById(R.id.logout);
}
Abdullah Tellioglu
  • 1,434
  • 1
  • 10
  • 26