从Fragment抛出NullPointerException

时间:2015-07-10 11:08:30

标签: java android android-fragments nullpointerexception

这是具有注销按钮的片段类代码。

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();
    }
}

但是当我运行代码时,它会抛出一个带有以下日志的NullPointerException。

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)

在编写代码时,它没有显示任何错误,所以我真的很想知道它有什么错误。根据日志,正是这部分引发了异常。

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

有关此问题的任何建议吗?

2 个答案:

答案 0 :(得分:1)

在您的代码中,您使用的是view.findViewById。这里viewnull。这就是你获得NullPointerException的原因。

而不是这段代码:

  

查看rootView = inflater.inflate(R.layout.fragment_mypage,container,   假);

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

试试这段代码:

View rootView = inflater.inflate(R.layout.fragment_mypage, container, false);
        logout = (Button) rootView.findViewById(R.id.logout);

答案 1 :(得分:0)

尝试这样

@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);
}