Skip to content Skip to sidebar Skip to footer

Error When Starting Lockscreen Android Project

I'm making a screen lock Button in a Fragment. Via this example: http://karanbalkar.com/2014/01/tutorial-71-implement-lock-screen-in-android/ My code: public class Tab1fragment ext

Solution 1:

I think you get a NullPointerException because btnEnableAdmin, btnDisableAdmin and btnLock are null. Try to call findViewById on the View you just inflate instead on the Activity:

@Override
public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    //return inflater.inflate(R.layout.tab1_layout, container, false);
    View rootView = inflater.inflate(R.layout.tab1_layout, container, false);
    mDevicePolicyManager = (DevicePolicyManager)getActivity().getSystemService(Context.DEVICE_POLICY_SERVICE);
    mComponentName = new ComponentName(getActivity(), MyAdminReceiver.class);
    Button btnEnableAdmin = (Button) rootView.findViewById(R.id.btnEnable);
    Button btnDisableAdmin = (Button) rootView.findViewById(R.id.btnDisable);
    Button btnLock = (Button) rootView.findViewById(R.id.btnLock);
    btnEnableAdmin.setOnClickListener(this);
    btnDisableAdmin.setOnClickListener(this);
    btnLock.setOnClickListener(this);

    return rootView;
}

Post a Comment for "Error When Starting Lockscreen Android Project"